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.

Constant Summary collapse

WORD_BOUNDARY =
%r{\s*\b\s*}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loose_tight_dictionary, 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(loose_tight_dictionary, record, read = nil)
  @loose_tight_dictionary = loose_tight_dictionary
  @record = record
  @read = read
end

Instance Attribute Details

#loose_tight_dictionaryObject (readonly)

:nodoc: all



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

def loose_tight_dictionary
  @loose_tight_dictionary
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 render=#{render} variants=#{variants.length}>"
end

#renderObject Also known as: to_str



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/loose_tight_dictionary/wrapper.rb', line 18

def render
  return @render if rendered?
  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.dup
  loose_tight_dictionary.stop_words.each do |stop_word|
    stop_word.apply! str
  end
  str.strip!
  @render = str.freeze
  @rendered = true
  @render
end

#rendered?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/loose_tight_dictionary/wrapper.rb', line 63

def rendered?
  @rendered == true
end

#similarity(other) ⇒ Object



50
51
52
# File 'lib/loose_tight_dictionary/wrapper.rb', line 50

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

#variantsObject



54
55
56
57
58
59
60
61
# File 'lib/loose_tight_dictionary/wrapper.rb', line 54

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

#wordsObject



46
47
48
# File 'lib/loose_tight_dictionary/wrapper.rb', line 46

def words
  @words ||= render.split(WORD_BOUNDARY)
end