deepcerulean/passive_record

View on GitHub
lib/passive_record/arithmetic_helpers.rb

Summary

Maintainability
A
0 mins
Test Coverage
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