Permalink
Browse files

Engine.match returns [path,vars]

  • Loading branch information...
1 parent 49df2fe commit a2a596c8f3a286043bd83e77f8992c23b1a2932e @mynyml committed May 18, 2009
@@ -12,7 +12,7 @@ class SimpleEngine
# Array of two elements:
#
# * index 0: first matching route
- # * index 1: array of values for route variables, in the order they were specified
+ # * index 1: array of values for the route's variables (in the order they were specified in the route)
#
# ===== Examples
#
@@ -21,9 +21,9 @@ class SimpleEngine
#
def self.match(path, routes)
routes.each do |route|
- return route if route == path
+ return [route, nil] if route == path
end
- nil
+ [nil, nil]
end
end
end
@@ -13,16 +13,20 @@ def add(*args, &action)
end
def match(verb, path)
- unless self.empty?
- routes = self.select {|route| route.verb == verb }
- paths = routes.map {|route| route.path }
+ none = [nil, nil]
+ return none if self.empty?
- path = self.engine.match(path, paths)
- routes.detect {|route| route.path == path }
- end
+ routes = self.select {|route| route.verb == verb }
+ paths = routes.map {|route| route.path }
+
+ path, vars = self.engine.match(path, paths)
+ return none if path.nil?
+
+ route = routes.detect {|route| route.path == path }
+ [route, vars]
end
- class Route < Array #:nodoc:
+ class Route #:nodoc:
attr_accessor :verb,:path,:options,:action
def initialize(verb, path, options, &action)
@@ -5,8 +5,8 @@
class SimpleEngineTest < Test::Unit::TestCase
test "matches simple paths" do
- Engine.match('/', ['/', '/foo']).should be('/')
- Engine.match('/foo', ['/', '/foo']).should be('/foo')
- Engine.match('/bar', ['/', '/foo']).should be(nil)
+ Engine.match('/', ['/', '/foo']).first.should be('/')
+ Engine.match('/foo', ['/', '/foo']).first.should be('/foo')
+ Engine.match('/bar', ['/', '/foo']).first.should be(nil)
end
end
View
@@ -33,6 +33,6 @@ def setup
App.get('/bar') { 'bar' }
App.routes.match(:get, '/foo').should_not be( nil )
- App.routes.match(:get, '/foo').action.call.should be('foo')
+ App.routes.match(:get, '/foo').first.action.call.should be('foo')
end
end
View
@@ -21,14 +21,14 @@ def setup
@routes.add(:get, '/bar', {}, &@action)
@routes.match(:get, '/bar').should_not be(nil)
- @routes.match(:get, '/bar').path.should be('/bar')
+ @routes.match(:get, '/bar').first.path.should be('/bar')
end
test "returns nil when no route matches" do
@routes.add(:get, '/foo', {}, &@action)
@routes.add(:get, '/bar', {}, &@action)
- @routes.match('/baz', :get).should be(nil)
+ @routes.match('/baz', :get).should be([nil,nil])
end
## engine

0 comments on commit a2a596c

Please sign in to comment.