Module: Utils::Patterns

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

Overview

A module that provides pattern matching functionality for file searching and text processing.

It includes classes for different types of pattern matching including fuzzy matching and regular expression matching.

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.

Parameters:

  • argument (String)

    the argument string that determines the pattern type

  • pattern_opts (Hash)

    the options to be passed to the pattern matcher constructor

  • default (String) (defaults to: ?f)

    the default pattern type to use when argument is nil or empty

Returns:

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



153
154
155
156
157
158
159
160
161
162
# File 'lib/utils/patterns.rb', line 153

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