Skip to content
This repository was archived by the owner on Sep 30, 2018. It is now read-only.

Commit 4c009d8

Browse files
committedNov 29, 2013
Support optional (named) segments inside Route paths
1 parent 9d8e9d1 commit 4c009d8

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
 

‎opal/vienna/router.rb

+3
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ class Route
4444
# Regexp for matching named splats in path
4545
SPLAT = /\\\*(\w+)/
4646

47+
OPTIONAL = /\\\((.*?)\\\)/
48+
4749
attr_reader :regexp, :named
4850

4951
def initialize(pattern, &handler)
5052
@named, @handler = [], handler
5153

5254
pattern = Regexp.escape pattern
55+
pattern = pattern.gsub OPTIONAL, "(?:$1)?"
5356

5457
pattern.gsub(NAMED) { |m| @named << m[1..-1] }
5558
pattern.gsub(SPLAT) { |m| @named << m[2..-1] }

‎spec/route_spec.rb

+28-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,33 @@
5757
subject.new('/:first/:second') { |params|
5858
params.should eq({ 'first' => 'woosh', 'second' => 'kapow' })
5959
}.match('/woosh/kapow')
60-
end
60+
end
61+
62+
describe "optional segment" do
63+
it "can match simple optional segments" do
64+
route = subject.new('/foo(/bar)')
65+
66+
expect(route.match('/foo')).to eq(true)
67+
expect(route.match('/foo/bar')).to eq(true)
68+
expect(route.match('/foo/baz')).to_not eq(true)
69+
end
70+
71+
it "can match a named part inside an optional part" do
72+
route = subject.new('/foo(/:bar)')
73+
expect(route.named).to eq(['bar'])
74+
end
75+
76+
it "returns set named part to nil when optional part not given" do
77+
subject.new('/foo(/:bar)') { |params|
78+
params.should eq({ 'bar' => nil })
79+
}.match('/foo')
80+
end
81+
82+
it "returns correct value for named param inside optional part" do
83+
subject.new('/foo(/:bar)') { |params|
84+
params.should eq({ 'bar' => '42' })
85+
}.match('/foo/42')
86+
end
87+
end
6188
end
6289
end
63-

0 commit comments

Comments
 (0)
This repository has been archived.