Sha256: c1f892e47d024b5c42912e2ec476149ee4b3d39f6a2ac3f653773957662e312e

Contents?: true

Size: 364 Bytes

Versions: 18

Compression:

Stored size: 364 Bytes

Contents

module PassiveRecord
  module ArithmeticHelpers
    def pluck(attr)
      all.map(&attr)
    end

    def sum(attr)
      pluck(attr).inject(&:+)
    end

    def average(attr)
      sum(attr) / count
    end

    def mode(attr)
      arr = pluck(attr)
      freq = arr.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
      arr.max_by { |v| freq[v] }
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
passive_record-0.4.15 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.14 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.13 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.12 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.11 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.10 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.8 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.7 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.6 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.5 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.4 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.3 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.2 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.1 lib/passive_record/arithmetic_helpers.rb
passive_record-0.4.0 lib/passive_record/arithmetic_helpers.rb
passive_record-0.3.22 lib/passive_record/arithmetic_helpers.rb
passive_record-0.3.21 lib/passive_record/arithmetic_helpers.rb
passive_record-0.3.20 lib/passive_record/arithmetic_helpers.rb