Class: FuzzyMatch::Wrapper

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

Overview

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

Constant Summary collapse

WORD_BOUNDARY =

“Foo’s” is one word “North-west” is just one word “Bolivia,” is just Bolivia

%r{\W*(?:\s+|$)}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fuzzy_match, record, literal = false) ⇒ Wrapper

Returns a new instance of Wrapper.



14
15
16
17
18
# File 'lib/fuzzy_match/wrapper.rb', line 14

def initialize(fuzzy_match, record, literal = false)
  @fuzzy_match = fuzzy_match
  @record = record
  @literal = literal
end

Instance Attribute Details

#fuzzy_matchObject (readonly)

Returns the value of attribute fuzzy_match.



9
10
11
# File 'lib/fuzzy_match/wrapper.rb', line 9

def fuzzy_match
  @fuzzy_match
end

#literalObject (readonly)

Returns the value of attribute literal.



11
12
13
# File 'lib/fuzzy_match/wrapper.rb', line 11

def literal
  @literal
end

#recordObject (readonly)

Returns the value of attribute record.



10
11
12
# File 'lib/fuzzy_match/wrapper.rb', line 10

def record
  @record
end

#renderedObject (readonly)

Returns the value of attribute rendered.



12
13
14
# File 'lib/fuzzy_match/wrapper.rb', line 12

def rendered
  @rendered
end

Instance Method Details

#inspectObject



20
21
22
# File 'lib/fuzzy_match/wrapper.rb', line 20

def inspect
  "#<FuzzyMatch::Wrapper render=#{render.inspect} variants=#{variants.length}>"
end

#readObject



24
25
26
# File 'lib/fuzzy_match/wrapper.rb', line 24

def read
  fuzzy_match.read unless literal
end

#renderObject Also known as: to_str



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fuzzy_match/wrapper.rb', line 28

def render
  @render ||= begin
    memo = 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
    fuzzy_match.stop_words.each do |stop_word|
      stop_word.apply! memo
    end
    memo.strip!
    @render = memo.freeze
  end
end

#similarity(other) ⇒ Object



58
59
60
# File 'lib/fuzzy_match/wrapper.rb', line 58

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

#variantsObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/fuzzy_match/wrapper.rb', line 62

def variants
  @variants ||= begin
    fuzzy_match.normalizers.inject([ render ]) do |memo, normalizer|
      if normalizer.apply? render
        memo << normalizer.apply(render)
      end
      memo
    end.uniq
  end
end

#wordsObject



54
55
56
# File 'lib/fuzzy_match/wrapper.rb', line 54

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