Class: Utils::Patterns::Pattern Abstract

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

Overview

This class is abstract.

Base class for pattern matching implementations.

This class serves as the foundation for various pattern matching strategies, providing common functionality for initializing patterns with character set filtering and case sensitivity options. It handles the core configuration and delegates specific matching behavior to subclasses.

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



31
32
33
34
35
36
37
# File 'lib/utils/patterns.rb', line 31

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



56
57
58
59
60
# File 'lib/utils/patterns.rb', line 56

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



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

def matcher
  @matcher
end