Module: PassiveRecord::ArithmeticHelpers

Included in:
PassiveRecord::Associations::HasManyRelation, Core::Query
Defined in:
lib/passive_record/arithmetic_helpers.rb

Instance Method Summary collapse

Instance Method Details

#average(attr) ⇒ Object



11
12
13
# File 'lib/passive_record/arithmetic_helpers.rb', line 11

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

#mode(attr) ⇒ Object



15
16
17
18
19
# File 'lib/passive_record/arithmetic_helpers.rb', line 15

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

#pluck(attr) ⇒ Object



3
4
5
# File 'lib/passive_record/arithmetic_helpers.rb', line 3

def pluck(attr)
  all.map(&attr)
end

#sum(attr) ⇒ Object



7
8
9
# File 'lib/passive_record/arithmetic_helpers.rb', line 7

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