Class: Utils::Patterns::Pattern Abstract
Overview
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
Instance Attribute Summary collapse
-
#matcher ⇒ Object
readonly
Returns the matcher object used for pattern matching.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Pattern
constructor
Initializes a new Pattern instance with the specified options.
-
#method_missing(*a, &b) ⇒ Object
The method_missing method delegates calls to the matcher object while handling UTF-8 encoding errors.
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.
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.
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..include?('invalid byte sequence in UTF-8') end |
Instance Attribute Details
#matcher ⇒ Object (readonly)
Returns the matcher object used for pattern matching.
42 43 44 |
# File 'lib/utils/patterns.rb', line 42 def matcher @matcher end |