Skip to content

Commit

Permalink
clean up regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Jan 10, 2021
1 parent c524621 commit fd76cdf
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/active_interaction/inputs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ module ActiveInteraction
# Holds inputs passed to the interaction.
class Inputs < DelegateClass(Hash)
class << self
GROUPED_INPUT_PATTERN = /\A(.+)\((\d+)i\)\z/.freeze
# matches inputs like "key(1i)"
GROUPED_INPUT_PATTERN = /
\A
(?<key>.+) # extracts "key"
\((?<index>\d+)i\) # extracts "1"
\z
/x.freeze
private_constant :GROUPED_INPUT_PATTERN

# Checking `syscall` is the result of what appears to be a bug in Ruby.
Expand All @@ -20,8 +26,8 @@ def process(inputs)
inputs.stringify_keys.sort.each_with_object({}) do |(k, v), h|
next if reserved?(k)

if (match = GROUPED_INPUT_PATTERN.match(k))
assign_to_grouped_input!(h, *match.captures, v)
if (group = GROUPED_INPUT_PATTERN.match(k))
assign_to_grouped_input!(h, group[:key], group[:index], v)
else
h[k.to_sym] = v
end
Expand Down

0 comments on commit fd76cdf

Please sign in to comment.