Class: Utils::Patterns::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/patterns.rb

Direct Known Subclasses

FuzzyPattern, RegexpPattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Pattern

Initializes a new Pattern instance with the specified options.

This method sets up the pattern configuration by storing the character set, case sensitivity flag, and pattern string. It validates that a pattern is provided and optionally filters the pattern characters based on the specified character set.

Parameters:

  • opts (Hash) (defaults to: {})

    a hash containing the pattern configuration options

Options Hash (opts):

  • :cset (String)

    the character set to filter pattern characters against

  • :icase (TrueClass, FalseClass)

    whether the pattern matching should be case sensitive

  • :pattern (String)

    the pattern string to be used for matching

Raises:

  • (ArgumentError)

    if the pattern option is not provided



17
18
19
20
21
22
23
# File 'lib/utils/patterns.rb', line 17

def initialize(opts = {})
  @cset    = opts[:cset]
  @icase   = opts[:icase]
  @pattern = opts[:pattern] or
    raise ArgumentError, "pattern option required"
  @pattern = @pattern.gsub(/[^#{@cset}]/, '') if @cset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*a, &b) ⇒ Object

The method_missing method delegates calls to the matcher object while handling UTF-8 encoding errors.

This method acts as a fallback handler for undefined method calls, forwarding them to the internal matcher object. It specifically catches ArgumentError exceptions related to invalid byte sequences in UTF-8 and re-raises them unless they match the expected error pattern.

Parameters:

  • a (Array)

    the arguments passed to the missing method

  • b (Proc)

    the block passed to the missing method

Returns:

  • (Object)

    the result of the delegated method call on the matcher



42
43
44
45
46
# File 'lib/utils/patterns.rb', line 42

def method_missing(*a, &b)
  @matcher.__send__(*a, &b)
rescue ArgumentError => e
  raise e unless e.message.include?('invalid byte sequence in UTF-8')
end

Instance Attribute Details

#matcherObject (readonly)

Returns the matcher object used for pattern matching.

Returns:

  • (Object)

    the matcher object that handles pattern matching operations



28
29
30
# File 'lib/utils/patterns.rb', line 28

def matcher
  @matcher
end