Class: Eco::Data::FuzzyMatch::Score

Inherits:
Struct
  • Object
show all
Defined in:
lib/eco/data/fuzzy_match/score.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#scoreObject

Returns the value of attribute score

Returns:

  • (Object)

    the current value of score



4
5
6
# File 'lib/eco/data/fuzzy_match/score.rb', line 4

def score
  @score
end

#totalObject

Returns the value of attribute total

Returns:

  • (Object)

    the current value of total



4
5
6
# File 'lib/eco/data/fuzzy_match/score.rb', line 4

def total
  @total
end

Instance Method Details

#increase(value = 1) ⇒ Object



17
18
19
20
21
# File 'lib/eco/data/fuzzy_match/score.rb', line 17

def increase(value = 1)
  self.score += value
  raise "Score #{self.score} (increase: #{value}) can't be greater than total #{self.total}" if self.score > self.total
  self.score
end

#increase_total(value) ⇒ Object



23
24
25
# File 'lib/eco/data/fuzzy_match/score.rb', line 23

def increase_total(value)
  self.total += value
end

#merge(scr) ⇒ Object

Merges 2 Score instance objects



34
35
36
# File 'lib/eco/data/fuzzy_match/score.rb', line 34

def merge(scr)
  Score.new(*values_at(:score, :total)).merge!(scr)
end

#merge!(scr) ⇒ Object



38
39
40
41
42
43
# File 'lib/eco/data/fuzzy_match/score.rb', line 38

def merge!(scr)
  raise "Expecting Score object. Given: #{scr.class}" unless scr.is_a?(Score)
  increase_total(scr.total)
  increase(scr.score)
  self
end

#percent(decimals = 3) ⇒ Object



13
14
15
# File 'lib/eco/data/fuzzy_match/score.rb', line 13

def percent(decimals = 3)
  (100 * ratio).round(decimals)
end

#ratio(decimals = 6) ⇒ Object



6
7
8
9
10
11
# File 'lib/eco/data/fuzzy_match/score.rb', line 6

def ratio(decimals = 6)
  tot = self.total; sc = self.score
  tot = tot && tot > 0 ? tot : 1
  sc  = sc  && sc  > 0 ? sc  : 0
  (sc.to_f / tot).round(decimals)
end

#values_at(*keys) ⇒ Object



27
28
29
30
31
# File 'lib/eco/data/fuzzy_match/score.rb', line 27

def values_at(*keys)
  keys.map do |key|
    self.send(key) if self.respond_to?(key)
  end
end