Sha256: 8c063ef0090c91698f5ef1d2c66dd779b5d2aed39fe4816fe2a318990c063f5a

Contents?: true

Size: 1.32 KB

Versions: 8

Compression:

Stored size: 1.32 KB

Contents

class FuzzyMatch
  # Records are the tokens that are passed around when doing scoring and optimizing.
  class Record #:nodoc: all
    # "Foo's" is one word
    # "North-west" is just one word
    # "Bolivia," is just Bolivia
    WORD_BOUNDARY = %r{\W*(?:\s+|$)}
    EMPTY = [].freeze
    BLANK = ''.freeze

    attr_reader :original
    attr_reader :read
    attr_reader :stop_words

    def initialize(original, options = {})
      @original = original
      @read = options[:read]
      @stop_words = options.fetch(:stop_words, EMPTY)
    end

    def inspect
      "w(#{clean.inspect})"
    end

    def clean
      @clean ||= begin
        memo = whole.dup
        stop_words.each do |stop_word|
          memo.gsub! stop_word, BLANK
        end
        memo.strip.freeze
      end
    end

    def words
      @words ||= clean.downcase.split(WORD_BOUNDARY).freeze
    end

    def similarity(other)
      Similarity.new self, other
    end

    def whole
      @whole ||= case read
      when ::NilClass
        original
      when ::Numeric, ::String
        original[read]
      when ::Proc
        read.call original
      when ::Symbol
        original.respond_to?(read) ? original.send(read) : original[read]
      else
        raise "Expected nil, a proc, or a symbol, got #{read.inspect}"
      end.to_s.strip.freeze
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/fuzzy_match-2.0.4/lib/fuzzy_match/record.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/fuzzy_match-2.0.4/lib/fuzzy_match/record.rb
fuzzy_match-2.1.0 lib/fuzzy_match/record.rb
fuzzy_match-2.0.4 lib/fuzzy_match/record.rb
fuzzy_match-2.0.3 lib/fuzzy_match/record.rb
fuzzy_match-2.0.2 lib/fuzzy_match/record.rb
fuzzy_match-2.0.1 lib/fuzzy_match/record.rb
fuzzy_match-2.0.0 lib/fuzzy_match/record.rb