Permalink
Browse files

let arbitrary methods through, doc for extend_route

  • Loading branch information...
1 parent 0f19677 commit 66923856026550d352855d688312907b76d4a472 @joshbuddy committed Sep 10, 2011
Showing with 15 additions and 4 deletions.
  1. +1 −1 Rakefile
  2. +7 −0 lib/http_router.rb
  3. +4 −0 lib/http_router/response.rb
  4. +2 −3 lib/http_router/route_proxy.rb
  5. +1 −0 test/test_misc.rb
View
@@ -133,7 +133,7 @@ namespace :test do
msg = expected.dup
msg << " was expected to be "
msg << "\#{__example_runner.inspect}"
- current_example << "raise \"#{msg.gsub('"', '\\"')}\" unless __example_runner.strip == #{expected}\n" if in_example
+ current_example << "raise \"#{msg.gsub('"', '\\"')}\" unless (__example_runner.respond_to?(:strip) ? __example_runner.strip : __example_runner) == #{expected}\n" if in_example
when ''
unless current_example.empty?
examples << current_example
View
@@ -80,6 +80,13 @@ def add_route(route)
route.router = self
end
+ # Extends the route class with custom features.
+ #
+ # Example:
+ # router = HttpRouter.new { extend_route { attr_accessor :controller } }
+ # router.add('/foo', :controller => :foo).to{|env| [200, {}, ['foo!']]}
+ # router.recognize(Rack::MockRequest.env_for('/foo')).first.route.controller
+ # # ==> :foo
def extend_route(&blk)
@route_class = Class.new(Route) if @route_class == Route
@route_class.class_eval(&blk)
@@ -6,6 +6,10 @@ def initialize(request, path)
@params = path.hashify_params(request.params)
end
+ def route
+ path.route
+ end
+
def param_values
request.params
end
@@ -22,10 +22,9 @@ def process_opts(opts)
opts.delete(:conditions)
end
opts.each do |k, v|
- case k
- when :name, :user_agent, :host, :path_for_generation
+ if @route.respond_to?(:"#{k}=")
@route.send(:"#{k}=", v)
- when *@route.significant_variable_names
+ elsif @route.significant_variable_names.include?(k)
@route.add_match_with(k => v)
else
send(:"add_#{k}", v)
View
@@ -92,6 +92,7 @@ def test_public_interface
assert methods.include?(:process_destination_path)
assert methods.include?(:rewrite_partial_path_info)
assert methods.include?(:rewrite_path_info)
+ assert methods.include?(:extend_route)
end
def test_to_s_and_inspect

0 comments on commit 6692385

Please sign in to comment.