Skip to content

Commit 52114b7

Browse files
authored
Merge pull request rails#35913 from baweaver/baweaver/actionpack/named_captures
Adds named_captures to MatchData to emulate Regex
2 parents d5f2deb + 79c1556 commit 52114b7

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

actionpack/lib/action_dispatch/journey/path/pattern.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ def captures
136136
Array.new(length - 1) { |i| self[i + 1] }
137137
end
138138

139+
def named_captures
140+
@names.zip(captures).to_h
141+
end
142+
139143
def [](x)
140144
idx = @offsets[x - 1] + x
141145
@match[idx]

actionpack/test/journey/path/pattern_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,15 @@ def test_match_literal_with_action_and_format
280280
assert_equal "list", match[1]
281281
assert_equal "rss", match[2]
282282
end
283+
284+
def test_named_captures
285+
path = Path::Pattern.from_string "/books(/:action(.:format))"
286+
287+
uri = "/books/list.rss"
288+
match = path =~ uri
289+
named_captures = { "action" => "list", "format" => "rss" }
290+
assert_equal named_captures, match.named_captures
291+
end
283292
end
284293
end
285294
end

0 commit comments

Comments
 (0)