From 452a03045ccb31c71dccc1a7ff3a474080cac0b3 Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Fri, 1 May 2009 10:29:30 -0400 Subject: [PATCH] Allow extensions to the routing system --- lib/sinatra/base.rb | 2 ++ test/routing_test.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index fd7a67a6..fcd9559a 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -807,6 +807,8 @@ module Sinatra end end [/^#{pattern}$/, keys] + elsif path.respond_to?(:keys) && path.respond_to?(:match) + [path, path.keys] elsif path.respond_to? :match [path, keys] else diff --git a/test/routing_test.rb b/test/routing_test.rb index 3b22ab23..20d2d15f 100644 --- a/test/routing_test.rb +++ b/test/routing_test.rb @@ -5,6 +5,22 @@ def route_def(pattern) mock_app { get(pattern) { } } end +class RegexpLookAlike + class MatchData + def captures + ["this", "is", "a", "test"] + end + end + + def match(string) + ::RegexpLookAlike::MatchData.new if string == "/this/is/a/test/" + end + + def keys + ["one", "two", "three", "four"] + end +end + class RoutingTest < Test::Unit::TestCase %w[get put post delete].each do |verb| it "defines #{verb.upcase} request handlers with #{verb}" do @@ -357,6 +373,22 @@ class RoutingTest < Test::Unit::TestCase assert_equal 'right on', body end + it 'supports regular expression look-alike routes' do + mock_app { + get(RegexpLookAlike.new) do + assert_equal 'this', params[:one] + assert_equal 'is', params[:two] + assert_equal 'a', params[:three] + assert_equal 'test', params[:four] + 'right on' + end + } + + get '/this/is/a/test/' + assert ok? + assert_equal 'right on', body + end + it 'raises a TypeError when pattern is not a String or Regexp' do assert_raise(TypeError) { mock_app { get(42){} }