Sha256: 13d595cefe80b509c2e0db82297dab86dc7798341a68479ccf33aa2c5fbac3a9

Contents?: true

Size: 650 Bytes

Versions: 3

Compression:

Stored size: 650 Bytes

Contents

class FuzzyMatch
  # A tightener just strips a string down to its core
  class Tightener
    attr_reader :regexp
    
    def initialize(regexp_or_str)
      @regexp = regexp_or_str.to_regexp
    end
    
    # A tightener applies when its regexp matches and captures a new (shorter) string
    def apply?(str)
      !!(regexp.match(str))
    end
    
    # The result of applying a tightener 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
      "#<Tightener regexp=#{regexp.inspect}>"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fuzzy_match-1.1.1 lib/fuzzy_match/tightener.rb
fuzzy_match-1.1.0 lib/fuzzy_match/tightener.rb
fuzzy_match-1.0.5 lib/fuzzy_match/tightener.rb