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

Version Path
eco-helpers-3.0.21 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.20 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.19 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.18 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.17 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.16 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.15 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.14 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.13 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.12 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.11 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.10 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.9 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.8 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.7 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.6 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.5 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.4 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.3 lib/eco/data/fuzzy_match/score.rb
eco-helpers-3.0.2 lib/eco/data/fuzzy_match/score.rb