Module: Utils::Patterns

Included in:
Finder, Grepper
Defined in:
lib/utils/patterns.rb

Defined Under Namespace

Classes: FuzzyPattern, Pattern, RegexpPattern

Instance Method Summary collapse

Instance Method Details

#choose(argument, pattern_opts, default: ?f) ⇒ Utils::Patterns::Pattern

Chooses and initializes a pattern matcher based on the provided argument and options.

This method selects between a regular expression pattern matcher and a fuzzy pattern matcher depending on the value of the argument parameter and the default configuration. It validates that the argument is either ‘r’ (regexp) or ‘f’ (fuzzy) and raises an error if an invalid value is provided.

Raises:

  • (ArgumentError)

    if the argument does not match ‘r’ or ‘f’ patterns and is not nil

  • (ArgumentError)

    if the pattern option is not provided to the pattern matcher constructor



114
115
116
117
118
119
120
121
122
123
# File 'lib/utils/patterns.rb', line 114

def choose(argument, pattern_opts, default: ?f)
  case argument
  when /^r/, (default == ?r ? nil : :not)
    RegexpPattern.new(pattern_opts)
  when /^f/, (default == ?f ? nil : :not)
    FuzzyPattern.new(pattern_opts)
  else
    raise ArgumentError, 'argument -p has to be f=fuzzy or r=regexp'
  end
end