Sha256: e18646885fa5559f2df96ca756aa06647319daa345cf6180d488d385476b9c1c
Contents?: true
Size: 1.22 KB
Versions: 130
Compression:
Stored size: 1.22 KB
Contents
module Eco module Data module FuzzyMatch class Score < Struct.new(:score, :total) 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 def percent(decimals = 3) (100 * ratio).round(decimals) end 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 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(scr) Score.new(*values_at(:score, :total)).merge!(scr) end def merge!(scr) raise "Expecting Score object. Given: #{scr.class}" unless scr.is_a?(Score) increase_total(scr.total) increase(scr.score) self end end end end end
Version data entries
130 entries across 130 versions & 1 rubygems