Skip to content
This repository has been archived by the owner on Sep 13, 2017. It is now read-only.

Commit

Permalink
literals in paths are recognized
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Mar 17, 2011
1 parent c0be4ca commit e172eaa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
17 changes: 13 additions & 4 deletions lib/journey/path/pattern.rb
Expand Up @@ -24,8 +24,6 @@ def names
end

class Matcher < Journey::Definition::Node::Visitor # :nodoc:
attr_reader :contents

def initialize scanner
@scanner = scanner
@contents = {}
Expand All @@ -40,7 +38,7 @@ def accept node

def visit_SEGMENT node
token, text = @scanner.next_token
raise unless token == :SLASH
raise "wrong token [#{token}, #{text}]" unless token == :SLASH
super
end

Expand All @@ -49,14 +47,25 @@ def visit_SYMBOL node
@contents[node.to_sym] = text
super
end

def visit_LITERAL node
token, text = @scanner.next_token
raise unless text == node.children
super
end

def visit_DOT node
token, text = @scanner.next_token
raise unless token == node.type
super
end
end

def =~ other
scanner = Journey::Definition::Scanner.new
scanner.scan_setup other
matcher = Matcher.new scanner
matcher.accept spec
matcher.contents
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/journey/router.rb
Expand Up @@ -26,9 +26,9 @@ def initialize options
@named_routes = {}
end

def add_route app, conditions, extras, name
def add_route app, conditions, defaults, name = nil
path = conditions[:path_info]
route = Route.new(app, path, nil, extras)
route = Route.new(app, path, nil, defaults)
routes << route
named_routes[name] = route if name
route
Expand All @@ -50,7 +50,7 @@ def recognize req
match_data = route.path =~ req.env['PATH_INFO']
end

yield(route, nil, match_data)
yield(route, nil, match_data.merge(route.extras))
end
end
end
10 changes: 5 additions & 5 deletions test/test_route.rb
Expand Up @@ -3,14 +3,14 @@
module Journey
class TestRoute < MiniTest::Unit::TestCase
def test_initialize
app = Object.new
path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
verb = Object.new
route = Route.new(app, path, verb)
app = Object.new
path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
defaults = Object.new
route = Route.new(app, path, nil, defaults)

assert_equal app, route.app
assert_equal path, route.path
assert_equal verb, route.verb
assert_equal defaults, route.extras
end

def test_connects_all_match
Expand Down
27 changes: 22 additions & 5 deletions test/test_router.rb
Expand Up @@ -8,7 +8,7 @@ def setup

def test_generate_id
path = Path::Pattern.new '/:controller(/:action)'
@router.add_route nil, {:path_info => path}, {}, nil
@router.add_route nil, {:path_info => path}, {}, {}

This comment has been minimized.

Copy link
@chancancode

chancancode Nov 23, 2014

Member

@tenderlove sorry for digging this up, but I came across this while working on rails#17725... do you remember why we are passing {} as the route name everywhere?

This comment has been minimized.

Copy link
@chancancode

chancancode Nov 23, 2014

Member

(As you can see above, the signature is def add_route app, conditions, defaults, name = nil)

This comment has been minimized.

Copy link
@tenderlove

tenderlove via email Nov 23, 2014

Author Member

This comment has been minimized.

Copy link
@chancancode

chancancode Nov 23, 2014

Member

No worries. It's not directly related to the ticket, I was just fixing some tests and not 100% sure about how this works. Seems like this parameter would then be used as the key for named_routes, etc, so that felt a bit odd


path, params = @router.generate(
:path_info, nil, {:id=>1, :controller=>"tasks", :action=>"show"}, {})
Expand All @@ -18,7 +18,7 @@ def test_generate_id

def test_generate_extra_params
path = Path::Pattern.new '/:controller(/:action)'
@router.add_route nil, {:path_info => path}, {}, nil
@router.add_route nil, {:path_info => path}, {}, {}

path, params = @router.generate(:path_info,
nil, { :id => 1,
Expand All @@ -32,7 +32,7 @@ def test_generate_extra_params

def test_generate_with_name
path = Path::Pattern.new '/:controller(/:action)'
@router.add_route nil, {:path_info => path}, {}, nil
@router.add_route nil, {:path_info => path}, {}, {}

path, params = @router.generate(:path_info,
"tasks",
Expand All @@ -47,7 +47,7 @@ def test_extras_are_removed_when_formatting
@router.add_route nil, { :path_info => path }, {
:controller => 'paths',
:action => 'show'
}, nil
}, {}
path = @router.generate(nil, nil, {
:controller =>"pages",
:id =>20,
Expand All @@ -64,7 +64,7 @@ def test_extras_are_removed_when_formatting
define_method("test_recognize_#{expected.keys.map(&:to_s).join('_')}") do
path = Path::Pattern.new "/:controller(/:action(/:id))"
app = Object.new
route = @router.add_route(app, { :path_info => path }, {}, nil)
route = @router.add_route(app, { :path_info => path }, {}, {})

env = rails_env 'PATH_INFO' => request_path
called = false
Expand All @@ -79,6 +79,23 @@ def test_extras_are_removed_when_formatting
end
end

def test_recognize_literal
path = Path::Pattern.new "/books(/:action(.:format))"
app = Object.new
route = @router.add_route(app, { :path_info => path }, {:controller => 'books'})

env = rails_env 'PATH_INFO' => '/books/list.rss'
expected = { :controller => 'books', :action => 'list', :format => 'rss' }
called = false
@router.recognize(env) do |r, _, params|
assert_equal route, r
assert_equal(expected, params)
called = true
end

assert called
end

private

RailsEnv = Struct.new(:env)
Expand Down

0 comments on commit e172eaa

Please sign in to comment.