Sha256: 1cdbc82bba520c685e171a4846e634bb24993611ccf5daa74410db15f71ba066

Contents?: true

Size: 1005 Bytes

Versions: 3

Compression:

Stored size: 1005 Bytes

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eco-helpers-2.0.19 lib/eco/data/fuzzy_match/score.rb
eco-helpers-2.0.18 lib/eco/data/fuzzy_match/score.rb
eco-helpers-2.0.17 lib/eco/data/fuzzy_match/score.rb