Sha256: eb736d2264f4c9e53245b6a8740b1fa7a929f563367d32d1500b48f7b440f369

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

class LooseTightDictionary
  # Wrappers are the tokens that are passed around when doing scoring and optimizing.
  class Wrapper #:nodoc: all
    attr_reader :parent
    attr_reader :record
    attr_reader :read

    def initialize(parent, record, read = nil)
      @parent = parent
      @record = record
      @read = read
    end

    def inspect
      "#<Wrapper to_str=#{to_str} variants=#{variants.length}>"
    end

    def to_str
      @to_str ||= case read
      when ::Proc
        read.call record
      when ::Symbol
        if record.respond_to?(read)
          record.send read
        else
          record[read]
        end
      when ::NilClass
        record
      else
        record[read]
      end.to_s
    end

    alias :to_s :to_str

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

    def variants
      @variants ||= parent.tighteners.inject([ to_str ]) do |memo, tightener|
        if tightener.apply? to_str
          memo.push tightener.apply(to_str)
        end
        memo
      end.uniq
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
loose_tight_dictionary-1.0.2 lib/loose_tight_dictionary/wrapper.rb
loose_tight_dictionary-1.0.1 lib/loose_tight_dictionary/wrapper.rb