From e833a8bbc0a86dfdb5cd65d2fd90d30e896e5754 Mon Sep 17 00:00:00 2001 From: Benoit de Chezelles Date: Sun, 23 Apr 2017 17:26:18 +0200 Subject: [PATCH] Move Router & Pipeline DSLs under Amber::Support::DSL module --- src/amber/pipe/pipeline.cr | 8 +------- src/amber/pipe/router.cr | 25 ++----------------------- src/amber/support/dsls.cr | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 30 deletions(-) create mode 100644 src/amber/support/dsls.cr diff --git a/src/amber/pipe/pipeline.cr b/src/amber/pipe/pipeline.cr index ad4bbfb26..0b64c8455 100644 --- a/src/amber/pipe/pipeline.cr +++ b/src/amber/pipe/pipeline.cr @@ -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) diff --git a/src/amber/pipe/router.cr b/src/amber/pipe/router.cr index a90e2821b..8f8ddedc2 100644 --- a/src/amber/pipe/router.cr +++ b/src/amber/pipe/router.cr @@ -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) @@ -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) @@ -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 diff --git a/src/amber/support/dsls.cr b/src/amber/support/dsls.cr new file mode 100644 index 000000000..80e839d71 --- /dev/null +++ b/src/amber/support/dsls.cr @@ -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