Skip to content

Commit

Permalink
cleanup, slightly cheaper arbitrary calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Hull committed Mar 21, 2011
1 parent 22444dd commit 9c53a8f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
1 change: 0 additions & 1 deletion http_router.gemspec
Expand Up @@ -25,7 +25,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'minitest', '~> 2.0.0'
s.add_development_dependency 'code_stats'
s.add_development_dependency 'rake'
s.add_development_dependency 'sinatra'
s.add_development_dependency 'rbench'
s.add_development_dependency 'phocus'
s.add_development_dependency 'bundler', '~> 1.0.0'
Expand Down
1 change: 1 addition & 0 deletions lib/http_router.rb
Expand Up @@ -57,6 +57,7 @@ def call(env, perform_call = true)
supported_methods = (@known_methods - [env['REQUEST_METHOD']]).select do |m|
test_env = Rack::Request.new(rack_request.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, false)
catch(:success) { @root[test_request] }
end
Expand Down
4 changes: 2 additions & 2 deletions lib/http_router/node.rb
Expand Up @@ -127,9 +127,9 @@ def add_request(opts)
next_requests
end

def add_arbitrary(blk, param_names)
def add_arbitrary(blk, allow_partial, param_names)
@arbitrary ||= []
@arbitrary << Arbitrary.new(@router, blk, param_names)
@arbitrary << Arbitrary.new(@router, allow_partial, blk, param_names)
@arbitrary.last
end

Expand Down
14 changes: 8 additions & 6 deletions lib/http_router/node/arbitrary.rb
@@ -1,15 +1,17 @@
class HttpRouter
class Node
class Arbitrary < Node
def initialize(router, blk, param_names)
@router, @blk, @param_names = router, blk, param_names
def initialize(router, allow_partial, blk, param_names)
@router, @allow_partial, @blk, @param_names = router, allow_partial, blk, param_names
end

def [](request)
request = request.clone
request.continue = proc { |state| destination(request) if state }
params = @param_names.nil? ? {} : Hash[@param_names.zip(request.params)]
@blk.call(request, params)
if request.path.empty? or (request.path.size == 1 and request.path[0] == '') or @allow_partial
request = request.clone
request.continue = proc { |state| destination(request) if state }
params = @param_names.nil? ? {} : Hash[@param_names.zip(request.params)]
@blk.call(request, params)
end
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/http_router/node/request.rb
Expand Up @@ -49,13 +49,11 @@ def add_linear(matcher)
end

def [](request)
matched = false
if @request_method
val = request.rack_request.send(@request_method.to_sym)
@linear.each { |(matcher, node)| node[request] if matcher === val }
@lookup[val][request] if @lookup.key?(val)
@catchall[request] if @catchall
matched = @lookup.key?(val) || !@catchall.nil?
else
super(request)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/http_router/route.rb
Expand Up @@ -236,7 +236,7 @@ def add_non_path_to_tree(node, path, names)
else
[node]
end
@arbitrary.each{|a| nodes.map!{|n| n.add_arbitrary(a, names)} } if @arbitrary
@arbitrary.each{|a| nodes.map!{|n| n.add_arbitrary(a, @match_partially, names)} } if @arbitrary
path_obj = Path.new(self, path, names)
nodes.each{|n| n.add_destination(path_obj)}
path_obj
Expand Down

0 comments on commit 9c53a8f

Please sign in to comment.