Skip to content

Commit 9103bcc

Browse files
committed
Added support for named captures
1 parent 7a22afc commit 9103bcc

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

lib/webmachine/dispatcher/route.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ def bind(tokens, bindings)
122122
when Regexp === spec.first
123123
matches = spec.first.match URI.decode(tokens.first)
124124
if matches
125-
bindings[:captures] = (bindings[:captures] || []) + matches.captures
125+
if spec.first.named_captures.empty?
126+
bindings[:captures] = (bindings[:captures] || []) + matches.captures
127+
else
128+
spec.first.named_captures.reduce(bindings) do |bindings, (name, idxs)|
129+
bindings[name.to_sym] = matches.captures[idxs.first-1]
130+
bindings
131+
end
132+
end
126133
else
127134
return false
128135
end

spec/webmachine/dispatcher/route_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ def call(request)
219219
expect(request.path_info).to eq({:captures => ["bar", "json"]})
220220
end
221221
end
222+
context "with named capture regex" do
223+
subject { described_class.new(['foo', :bar, /(?<baz>[^.]+)\.(?<format>.*)/], resource) }
224+
let(:uri) { URI.parse("http://localhost:8080/foo/bar/baz.json") }
225+
226+
it "should assign the captures path variables" do
227+
expect(request.path_info).to eq({bar: 'bar', baz: 'baz', format: "json"})
228+
end
229+
end
222230

223231
context "with a splat" do
224232
subject { described_class.new(['foo', :*], resource) }

0 commit comments

Comments
 (0)