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



19
20
21
22
23
24
25
26
# File 'lib/eco/data/fuzzy_match/score.rb', line 19

def increase(value = 1)
  self.score += value

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

  self.score
end

#increase_total(value) ⇒ Object



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

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

#merge(scr) ⇒ Object

Merges 2 Score instance objects



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

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

#merge!(scr) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/eco/data/fuzzy_match/score.rb', line 43

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



15
16
17
# File 'lib/eco/data/fuzzy_match/score.rb', line 15

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

#ratio(decimals = 6) ⇒ Object



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

def ratio(decimals = 6)
  tot = total
  sc  = score

  tot = 1 unless tot&.positive?
  sc  = 0 unless sc&.positive?

  (sc.to_f / tot).round(decimals)
end

#values_at(*keys) ⇒ Object



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

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