Skip to content

Commit

Permalink
Refactor routing & pipeline dsl (amberframework#17)
Browse files Browse the repository at this point in the history
* Move Router & Pipeline DSLs under Amber::Support::DSL module
* Make the DSLs tied to a particular Router/Pipeline instance
* Fix router spec
  • Loading branch information
bew authored and eliasjpr committed Apr 26, 2017
1 parent c371a02 commit 8f41f13
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 67 deletions.
2 changes: 1 addition & 1 deletion spec/amber/pipe/router_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Amber
describe "#call" do
it "raises exception when route not found" do
router = Router.instance
request = HTTP::Request.new("GET", "/hello/world")
request = HTTP::Request.new("GET", "/bad/route")

expect_raises Exceptions::RouteNotFound do
create_request_and_return_io(router, request)
Expand Down
12 changes: 8 additions & 4 deletions src/amber.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ module Amber
with self yield self
end

def routes(&block)
router.draw(&block)
macro routes
router.draw do
{{yield}}
end
end

def pipeline(valve : Symbol, &block)
handler.build valve, &block
macro pipeline(valve)
handler.build {{valve}} do
{{yield}}
end
end

def handler
Expand Down
2 changes: 1 addition & 1 deletion src/amber/pipe/pipeline.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Amber
def build(valve : Symbol, &block)
@valve = valve
@pipeline[@valve] = [] of HTTP::Handler unless @pipeline.key? @valve
with self yield
with Support::DSL::Pipeline.new(self) yield
end

def plug(pipe : HTTP::Handler)
Expand Down
4 changes: 2 additions & 2 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 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 Down
59 changes: 0 additions & 59 deletions src/amber/routes_macros.cr

This file was deleted.

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 8f41f13

Please sign in to comment.