Skip to content

Commit

Permalink
Move Router & Pipeline DSLs under Amber::Support::DSL module
Browse files Browse the repository at this point in the history
  • Loading branch information
bew committed Apr 23, 2017
1 parent 1aaf6ea commit e833a8b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
8 changes: 1 addition & 7 deletions src/amber/pipe/pipeline.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ module Amber
def build(valve : Symbol, &block)
@valve = valve
@pipeline[@valve] = [] of HTTP::Handler unless @pipeline.key? @valve
with PipelineDSL.new(self) yield
end

record PipelineDSL, pipeline : Pipeline do
def plug(pipe)
pipeline.plug pipe
end
with Support::DSL::Pipeline.new(self) yield
end

def plug(pipe : HTTP::Handler)
Expand Down
25 changes: 2 additions & 23 deletions src/amber/pipe/router.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Amber

# This registers all the routes for the application
def draw
with RouterDSL.new(self) yield
with Support::DSL::Router.new(self) yield
end

def add(route : Route)
Expand All @@ -46,7 +46,7 @@ module Amber
end

private def merge_params(params, context)
params.each { |k,v| context.params.add(k.to_s, v) }
params.each { |k, v| context.params.add(k.to_s, v) }
end

private def match(http_verb, resource) : Radix::Result(Amber::Route)
Expand All @@ -61,27 +61,6 @@ module Amber
trail = build_node(:HEAD, route.resource)
@routes.add(trail, route)
end

record RouterDSL, router : Router do
macro route(verb, resource, controller, handler, pipeline)
%ctrl = {{controller.id}}.new
%action = ->%ctrl.{{handler.id}}
%verb = {{verb.upcase.id.stringify}}
%route = Amber::Route.new(%verb, {{resource}}, %ctrl, %action, {{pipeline}})

router.add(%route)
end

{% for verb in {:get, :post, :put, :delete, :options, :head, :trace, :connect} %}

macro {{verb.id}}(*args)
route {{verb}}, \{{*args}}
end

{% end %}

end

end
end
end
26 changes: 26 additions & 0 deletions src/amber/support/dsls.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Amber::Support::DSL
record Pipeline, pipeline : Pipe::Pipeline do
def plug(pipe)
pipeline.plug pipe
end
end

record Router, router : Pipe::Router do
macro route(verb, resource, controller, handler, pipeline)
%ctrl = {{controller.id}}.new
%action = ->%ctrl.{{handler.id}}
%verb = {{verb.upcase.id.stringify}}
%route = Amber::Route.new(%verb, {{resource}}, %ctrl, %action, {{pipeline}})

router.add(%route)
end

{% for verb in {:get, :post, :put, :delete, :options, :head, :trace, :connect} %}

macro {{verb.id}}(*args)
route {{verb}}, \{{*args}}
end

{% end %}
end
end

0 comments on commit e833a8b

Please sign in to comment.