Class: LooseTightDictionary::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/loose_tight_dictionary/wrapper.rb

Overview

Wrappers are the tokens that are passed around when doing scoring and optimizing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, record, read = nil) ⇒ Wrapper

Returns a new instance of Wrapper.



8
9
10
11
12
# File 'lib/loose_tight_dictionary/wrapper.rb', line 8

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

Instance Attribute Details

#parentObject (readonly)

:nodoc: all



4
5
6
# File 'lib/loose_tight_dictionary/wrapper.rb', line 4

def parent
  @parent
end

#readObject (readonly)

Returns the value of attribute read.



6
7
8
# File 'lib/loose_tight_dictionary/wrapper.rb', line 6

def read
  @read
end

#recordObject (readonly)

Returns the value of attribute record.



5
6
7
# File 'lib/loose_tight_dictionary/wrapper.rb', line 5

def record
  @record
end

Instance Method Details

#inspectObject



14
15
16
# File 'lib/loose_tight_dictionary/wrapper.rb', line 14

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

#similarity(other) ⇒ Object



37
38
39
# File 'lib/loose_tight_dictionary/wrapper.rb', line 37

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

#to_strObject Also known as: to_s



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/loose_tight_dictionary/wrapper.rb', line 18

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

#variantsObject



41
42
43
44
45
46
47
48
# File 'lib/loose_tight_dictionary/wrapper.rb', line 41

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