Class: Utils::Patterns::FuzzyPattern
- Defined in:
- lib/utils/patterns.rb
Overview
A fuzzy pattern matcher that performs partial string matching while preserving character order.
This class implements a pattern matching strategy that allows for flexible matching of strings where the characters of the search pattern appear in sequence within the target string, but not necessarily consecutively. It is particularly useful for finding text patterns with potential typos or when only partial information about the target is available.
Instance Attribute Summary
Attributes inherited from Pattern
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ FuzzyPattern
constructor
Initializes a fuzzy pattern matcher by processing the pattern string and compiling it into a regular expression.
Methods inherited from Pattern
Constructor Details
#initialize(opts = {}) ⇒ FuzzyPattern
Initializes a fuzzy pattern matcher by processing the pattern string and compiling it into a regular expression.
This method takes the configured pattern string and converts it into a regular expression that can match strings in a fuzzy manner, allowing for partial matches while preserving the order of characters. It handles case sensitivity based on the configuration.
89 90 91 92 93 94 95 96 97 |
# File 'lib/utils/patterns.rb', line 89 def initialize(opts = {}) super r = @pattern.split(//).grep(/[[:print:]]/).map { |x| "(#{Regexp.quote(x)})" } * '.*?' @matcher = Regexp.new( "\\A(?:.*/.*?#{r}|.*#{r})", @icase ? Regexp::IGNORECASE : 0 ) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Utils::Patterns::Pattern