From 32814f8d6feb79df886e676412e7a62b44448994 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sun, 30 Oct 2011 12:53:18 -0700 Subject: [PATCH] refactor process_route --- lib/sinatra/base.rb | 24 ++++++++++++------------ test/routing_test.rb | 3 +-- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index af3a7c0a..3ce221e4 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -797,22 +797,22 @@ module Sinatra # # Returns pass block. def process_route(pattern, keys, conditions, block = nil, values = []) - @original_params ||= @params route = @request.path_info route = '/' if route.empty? and not settings.empty_path_info? - if match = pattern.match(route) - 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 } - @params = @original_params.merge(params) - @block_params = values - catch(:pass) do - conditions.each { |c| throw :pass if c.bind(self).call == false } - block ? block[self, @block_params] : yield(self, @block_params) - end + return unless match = pattern.match(route) + values += match.captures.to_a.map { |v| force_encoding URI.decode(v) if v } + + if values.any? + original, @params = params, params.merge('splat' => [], 'captures' => values) + keys.zip(values) { |k,v| (@params[k] ||= '') << v if v } + end + + catch(:pass) do + conditions.each { |c| throw :pass if c.bind(self).call == false } + block ? block[self, values] : yield(self, values) end ensure - @params = @original_params + @params = original if original end # No matching route was found or all routes passed. The default diff --git a/test/routing_test.rb b/test/routing_test.rb index a29b3c0f..e3f4d0be 100644 --- a/test/routing_test.rb +++ b/test/routing_test.rb @@ -370,8 +370,7 @@ class RoutingTest < Test::Unit::TestCase "firefox" => {"engine" => {"name"=>"spidermonkey", "version"=>"1.7.0"}}, "chrome" => {"engine" => {"name"=>"V8", "version"=>"1.0"}} }, - "paste" => {"name"=>"hello world", "syntax"=>"ruby"}, - "splat"=>[], "captures"=>[] + "paste" => {"name"=>"hello world", "syntax"=>"ruby"} } mock_app { get '/foo' do