Skip to content
Browse files

clarity around recognize

  • Loading branch information...
1 parent 077d9b6 commit 83b4875fa2cd36cbc355be103a837e9382974abf @joshbuddy committed
Showing with 34 additions and 37 deletions.
  1. +16 −15 lib/http_router.rb
  2. +3 −3 lib/http_router/node/path.rb
  3. +3 −11 lib/http_router/request.rb
  4. +12 −8 test/test_misc.rb
View
31 lib/http_router.rb
@@ -117,15 +117,16 @@ def put(path, opts = {}, &app); add_with_request_method(path, :put, opts, &app);
# Performs recoginition without actually calling the application and returns an array of all
# matching routes or nil if no match was found.
def recognize(env)
- call(env, false)
+ response = call(env, true)
+ response && block_given? ? response.each{|r| yield r} : response
end
# Rack compatible #call. If matching route is found, and +dest+ value responds to #call, processing will pass to the matched route. Otherwise,
# the default application will be called. The router will be available in the env under the key <tt>router</tt>. And parameters matched will
# be available under the key <tt>router.params</tt>.
- def call(env, perform_call = true)
+ def call(env, as_iterator = false)
compile
- call(env, perform_call)
+ call(env, as_iterator)
end
alias_method :compiling_call, :call
@@ -221,7 +222,7 @@ def no_response(env)
test_env = ::Rack::Request.new(env.clone)
test_env.env['REQUEST_METHOD'] = m
test_env.env['_HTTP_ROUTER_405_TESTING_ACCEPTANCE'] = true
- test_request = Request.new(test_env.path_info, test_env, 405)
+ test_request = Request.new(test_env.path_info, test_env, true)
@root[test_request]
!test_request.matches.empty?
end
@@ -245,18 +246,18 @@ def compile
routes.sort!{|r1, r2| r2.max_param_count <=> r1.max_param_count }
end
- instance_eval "undef :path; alias :path :raw_path;
- undef :url; alias :url :raw_url;
- undef :url_ns; alias :url_ns :raw_url_ns;
+ instance_eval "undef :path; alias :path :raw_path
+ undef :url; alias :url :raw_url
+ undef :url_ns; alias :url_ns :raw_url_ns
undef :call; alias :call :raw_call", __FILE__, __LINE__
@compiled = true
end
def uncompile
return unless @compiled
- instance_eval "undef :path; alias :path :compiling_path;
- undef :url; alias :url :compiling_url;
- undef :url_ns; alias :url_ns :compiling_url_ns;
+ instance_eval "undef :path; alias :path :compiling_path
+ undef :url; alias :url :compiling_url
+ undef :url_ns; alias :url_ns :compiling_url_ns
undef :call; alias :call :compiling_call", __FILE__, __LINE__
@root.uncompile
@compiled = false
@@ -286,14 +287,14 @@ def raw_path(route, *args)
raise(InvalidRouteException.new "No route (path) could be generated for #{route.inspect}")
end
- def raw_call(env, perform_call = true)
+ def raw_call(env, as_iterator = false)
rack_request = ::Rack::Request.new(env)
- request = Request.new(rack_request.path_info, rack_request, perform_call)
+ request = Request.new(rack_request.path_info, rack_request, as_iterator)
response = catch(:success) { @root[request] }
- if perform_call
- response or no_response(env)
- else
+ if as_iterator
request.matches.empty? ? nil : request.matches
+ else
+ response or no_response(env)
end
end
View
6 lib/http_router/node/path.rb
@@ -26,7 +26,9 @@ def to_code
end" if router.redirect_trailing_slash?}
#{"if request.path.empty?#{" or (request.path.size == 1 and request.path.first == '')" if router.ignore_trailing_slash?}" unless route.match_partially}
- if request.perform_call
+ if request.as_iterator
+ request.matched_route(Response.new(request, #{path_ivar}))
+ else
env = request.rack_request.dup.env
env['router.request'] = request
env['router.params'] ||= {}
@@ -34,8 +36,6 @@ def to_code
@router.rewrite#{"_partial" if route.match_partially}_path_info(env, request)
response = @router.process_destination_path(#{path_ivar}, env)
router.pass_on_response(response) ? throw(:pass) : throw(:success, response)
- else
- request.matched_route(Response.new(request, #{path_ivar}))
end
#{"end" unless route.match_partially}
end
View
14 lib/http_router/request.rb
@@ -1,10 +1,10 @@
class HttpRouter
class Request
attr_accessor :path, :params, :rack_request, :extra_env, :continue, :passed_with
- attr_reader :matches
+ attr_reader :as_iterator, :matches
alias_method :rack, :rack_request
- def initialize(path, rack_request, perform_call)
- @rack_request, @perform_call = rack_request, perform_call
+ def initialize(path, rack_request, as_iterator)
+ @rack_request, @as_iterator = rack_request, as_iterator
@path = URI.unescape(path).split(/\//)
@path.shift if @path.first == ''
@path.push('') if path[-1] == ?/
@@ -21,14 +21,6 @@ def matched_route(response)
@matches << response
end
- def perform_call
- @perform_call == true
- end
-
- def testing_405?
- @perform_call == 405
- end
-
def to_s
"request path, #{path.inspect}"
end
View
20 test/test_misc.rb
@@ -9,8 +9,8 @@ def test_cloning
assert_equal nil, r1.recognize(Rack::Request.new(Rack::MockRequest.env_for('/test2')))
assert r2.recognize(Rack::MockRequest.env_for('/test2'))
- assert_equal r1.routes.first, r1.named_routes[:test_route].first
- assert_equal r2.routes.first, r2.named_routes[:test_route].first
+ assert_equal r1.routes.size, 1
+ assert_equal r2.routes.size, 2
r1.add('/another', :name => :test).to(:test2)
@@ -59,6 +59,16 @@ def test_multi_name_gen
assert_equal '/name/category', r.path(:index, 'name', 'category')
end
+ def test_yielding_from_recognize
+ r = HttpRouter.new
+ r1 = r.add('/:name').default_destination.route
+ r2 = r.add('/:name').default_destination.route
+ r3 = r.add('/:name').default_destination.route
+ matches = []
+ r.recognize(Rack::MockRequest.env_for('/test')) { |r| matches << r.route }
+ assert_equal [r1, r2, r3], matches
+ end
+
def test_regex_generation
r = router
r.add(%r|/test/.*|, :path_for_generation => '/test/:variable', :name => :route).default_destination
@@ -73,12 +83,6 @@ def test_too_many_params
assert_raises(HttpRouter::InvalidRouteException) { r.path(:route) }
end
- def test_too_many_args
- r = router
- r.add('/', :name => :route).default_destination
- assert_raises(HttpRouter::TooManyParametersException) { r.path(:route, "hi") }
- end
-
def test_public_interface
methods = HttpRouter.public_instance_methods.map(&:to_sym)
assert methods.include?(:url_mount)

0 comments on commit 83b4875

Please sign in to comment.
Something went wrong with that request. Please try again.