1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Adds named_captures to MatchData to emulate Regex

This change adds a `named_captures` method to
`ActionDispatch::Journey::Path::MatchData` in order to emulate a similar
method present on `Regex`'s `MatchData` present in Ruby core.

This method can be useful for introspection of routes without the need
to use `zip` while testing or developing in Rails core.
This commit is contained in:
Brandon Weaver 2019-04-09 11:35:09 -07:00
parent dd58d040b2
commit 79c15566da
2 changed files with 13 additions and 0 deletions

View file

@ -136,6 +136,10 @@ module ActionDispatch
Array.new(length - 1) { |i| self[i + 1] }
end
def named_captures
@names.zip(captures).to_h
end
def [](x)
idx = @offsets[x - 1] + x
@match[idx]

View file

@ -280,6 +280,15 @@ module ActionDispatch
assert_equal "list", match[1]
assert_equal "rss", match[2]
end
def test_named_captures
path = Path::Pattern.from_string "/books(/:action(.:format))"
uri = "/books/list.rss"
match = path =~ uri
named_captures = { "action" => "list", "format" => "rss" }
assert_equal named_captures, match.named_captures
end
end
end
end