From 79c15566da3f267d66cfd6cc8e2257b6aacc0e38 Mon Sep 17 00:00:00 2001 From: Brandon Weaver Date: Tue, 9 Apr 2019 11:35:09 -0700 Subject: 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. --- actionpack/lib/action_dispatch/journey/path/pattern.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'actionpack/lib/action_dispatch/journey/path') diff --git a/actionpack/lib/action_dispatch/journey/path/pattern.rb b/actionpack/lib/action_dispatch/journey/path/pattern.rb index 697f5b9d8b..a968df5f19 100644 --- a/actionpack/lib/action_dispatch/journey/path/pattern.rb +++ b/actionpack/lib/action_dispatch/journey/path/pattern.rb @@ -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] -- cgit v1.2.3