Module: Mongoid::Contextual::Aggregable::Memory
- Included in:
- Memory
- Defined in:
- lib/mongoid/contextual/aggregable/memory.rb
Overview
Contains behavior for aggregating values in memory.
Instance Method Summary collapse
-
#avg(field) ⇒ Float
Get the average value of the provided field.
-
#max(field = nil) ⇒ Float, Document
Get the max value of the provided field.
-
#min(field = nil) ⇒ Float, Document
Get the min value of the provided field.
-
#sum(field = nil) ⇒ Float
Get the sum value of the provided field.
Instance Method Details
#avg(field) ⇒ Float
Get the average value of the provided field.
20 21 22 |
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 20 def avg(field) count > 0 ? sum(field).to_f / count.to_f : nil end |
#max(field = nil) ⇒ Float, Document
Get the max value of the provided field. If provided a block, will return the Document with the greatest value for the field, in accordance with Ruby’s enumerable API.
42 43 44 |
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 42 def max(field = nil) block_given? ? super() : aggregate_by(field, :max_by) end |
#min(field = nil) ⇒ Float, Document
Get the min value of the provided field. If provided a block, will return the Document with the smallest value for the field, in accordance with Ruby’s enumerable API.
64 65 66 |
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 64 def min(field = nil) block_given? ? super() : aggregate_by(field, :min_by) end |
#sum(field = nil) ⇒ Float
Get the sum value of the provided field. If provided a block, will return the sum in accordance with Ruby’s enumerable API.
82 83 84 85 86 87 88 |
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 82 def sum(field = nil) if block_given? super() else count > 0 ? super(0) { |doc| doc.public_send(field) } : 0 end end |