Sha256: 986782b4ef97006be55c44b8e77c09e06ada4413633a28f467944a488abf797b

Contents?: true

Size: 667 Bytes

Versions: 3

Compression:

Stored size: 667 Bytes

Contents

class FuzzyMatch
  # A normalizer just strips a string down to its core
  class Normalizer
    attr_reader :regexp
    
    def initialize(regexp_or_str)
      @regexp = regexp_or_str.to_regexp
    end
    
    # A normalizer applies when its regexp matches and captures a new (shorter) string
    def apply?(str)
      !!(regexp.match(str))
    end
    
    # The result of applying a normalizer is just all the captures put together.
    def apply(str)
      if match_data = regexp.match(str)
        match_data.captures.join
      else
        str
      end
    end
    
    def inspect
      "#<FuzzyMatch::Normalizer regexp=#{regexp.inspect}>"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fuzzy_match-1.3.1 lib/fuzzy_match/normalizer.rb
fuzzy_match-1.3.0 lib/fuzzy_match/normalizer.rb
fuzzy_match-1.2.2 lib/fuzzy_match/normalizer.rb