1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

refactor process_route

This commit is contained in:
Konstantin Haase 2011-10-30 12:53:18 -07:00
parent 1e0477f570
commit 32814f8d6f
2 changed files with 13 additions and 14 deletions

View file

@ -797,22 +797,22 @@ module Sinatra
# #
# Returns pass block. # Returns pass block.
def process_route(pattern, keys, conditions, block = nil, values = []) def process_route(pattern, keys, conditions, block = nil, values = [])
@original_params ||= @params
route = @request.path_info route = @request.path_info
route = '/' if route.empty? and not settings.empty_path_info? route = '/' if route.empty? and not settings.empty_path_info?
if match = pattern.match(route) return unless match = pattern.match(route)
values += match.captures.to_a.map { |v| force_encoding URI.decode(v) if v } values += match.captures.to_a.map { |v| force_encoding URI.decode(v) if v }
params = {'splat' => [], 'captures' => values}
keys.zip(values) { |k,v| (params[k] ||= '') << v if v } if values.any?
@params = @original_params.merge(params) original, @params = params, params.merge('splat' => [], 'captures' => values)
@block_params = values keys.zip(values) { |k,v| (@params[k] ||= '') << v if v }
end
catch(:pass) do catch(:pass) do
conditions.each { |c| throw :pass if c.bind(self).call == false } conditions.each { |c| throw :pass if c.bind(self).call == false }
block ? block[self, @block_params] : yield(self, @block_params) block ? block[self, values] : yield(self, values)
end
end end
ensure ensure
@params = @original_params @params = original if original
end end
# No matching route was found or all routes passed. The default # No matching route was found or all routes passed. The default

View file

@ -370,8 +370,7 @@ class RoutingTest < Test::Unit::TestCase
"firefox" => {"engine" => {"name"=>"spidermonkey", "version"=>"1.7.0"}}, "firefox" => {"engine" => {"name"=>"spidermonkey", "version"=>"1.7.0"}},
"chrome" => {"engine" => {"name"=>"V8", "version"=>"1.0"}} "chrome" => {"engine" => {"name"=>"V8", "version"=>"1.0"}}
}, },
"paste" => {"name"=>"hello world", "syntax"=>"ruby"}, "paste" => {"name"=>"hello world", "syntax"=>"ruby"}
"splat"=>[], "captures"=>[]
} }
mock_app { mock_app {
get '/foo' do get '/foo' do