module Eco module Data module FuzzyMatch class Score < Struct.new(:score, :total) def ratio(decimals = 6) ((score || 0).to_f / (total || 1)).round(decimals) end def percent(decimals = 3) (100 * ratio).round(decimals) end def increase(value = 1) self.score += value end def increase_total(value) self.total += value end def values_at(*keys) keys.map do |key| self.send(key) if self.respond_to?(key) end end # Merges 2 Score instance objects def merge(value) Score.new(*values_at(:score, :total)).merge!(value) end def merge!(value) raise "Expecting Score object. Given: #{value.class}" unless value.is_a?(Score) increase(value.score) increase_total(value.total) self end end end end end