Skip to content

Commit

Permalink
Added Merb::Router.extensions as an official API.
Browse files Browse the repository at this point in the history
Merb::Router.extensions is now the official way to add functionality
to the router.
  • Loading branch information
Carl Lerche committed Oct 12, 2008
1 parent 209ca50 commit 72f7c7e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
36 changes: 34 additions & 2 deletions lib/merb-core/dispatch/router.rb
Expand Up @@ -236,8 +236,40 @@ def resource(*args)
route.generate(params, defaults)
end

private

# Add functionality to the router. This can be in the form of
# including a new module or directly defining new methods.
#
# ==== Parameters
# &block<Block>::
# A block of code used to extend the route builder with. This
# can be including a module or directly defining some new methods
# that should be available to building routes.
#
# ==== Example
# Merb::Router.extensions do
# def domain(name, domain, options={}, &block)
# match(:domain => domain).namespace(name, :path => nil, &block)
# end
# end
#
# In this case, a method 'domain' will be available to the route builder
# which will create namespaces around domains instead of path prefixes.
#
# This can then be used as follows.
#
# Merb::Router.prepare do
# domain(:admin, "my-admin.com") do
# # ... routes come here ...
# end
# end
# ---
# @api public
def extensions(&block)
Router::Behavior.class_eval(&block)
end

private

# Compiles the routes and creates the +match+ method.
#
# @api private
Expand Down
10 changes: 5 additions & 5 deletions lib/merb-core/dispatch/router/resources.rb
Expand Up @@ -272,11 +272,7 @@ def resource_block(builders, &block)
end

end # Resources

class Behavior
include Resources
end


# Adding the collection and member methods to behavior
class ResourceBehavior < Behavior #:nodoc:

Expand All @@ -301,5 +297,9 @@ def member(action, options = {})
end

end

class Behavior
include Resources
end
end
end
18 changes: 17 additions & 1 deletion spec/public/router/router_spec.rb
Expand Up @@ -145,7 +145,7 @@
end

it "should not be able to match routes anymore" do
lambda { route_for("/users") }
lambda { route_for("/users") }.should raise_error(Merb::Router::NotCompiledError)
end

end
Expand All @@ -157,5 +157,21 @@
end

end

describe "#extensions" do
it "should be able to extend the router" do
Merb::Router.extensions do
def hello_world
match("/hello").to(:controller => "world")
end
end

Merb::Router.prepare do
hello_world
end

route_for("/hello").should have_route(:controller => "world")
end
end

end

0 comments on commit 72f7c7e

Please sign in to comment.