Skip to content
Browse files

Allow extensions to the routing system

  • Loading branch information...
1 parent 0a6927c commit 452a03045ccb31c71dccc1a7ff3a474080cac0b3 @sporkmonger sporkmonger committed with rtomayko May 1, 2009
Showing with 34 additions and 0 deletions.
  1. +2 −0 lib/sinatra/base.rb
  2. +32 −0 test/routing_test.rb
View
2 lib/sinatra/base.rb
@@ -807,6 +807,8 @@ def compile(path)
end
end
[/^#{pattern}$/, keys]
+ elsif path.respond_to?(:keys) && path.respond_to?(:match)
+ [path, path.keys]
elsif path.respond_to? :match
[path, keys]
else
View
32 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){} }

0 comments on commit 452a030

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