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
- #aggregates(field) ⇒ Hash deprecated Deprecated.
- #avg(field) ⇒ Numeric deprecated Deprecated.
- #max(field = nil) ⇒ Numeric | Document deprecated Deprecated.
- #min(field = nil) ⇒ Numeric | Document deprecated Deprecated.
- #sum(field = nil) ⇒ Numeric deprecated Deprecated.
Instance Method Details
#aggregates(field) ⇒ Hash
Deprecated.
Get all the aggregate values for the provided field. Provided for interface consistency with Aggregable::Mongo.
19 20 21 22 23 |
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 19 def aggregates(field) %w(count sum avg min max).each_with_object({}) do |method, hash| hash[method] = send(method, field) end end |
#avg(field) ⇒ Numeric
Deprecated.
Get the average value of the provided field.
35 36 37 38 39 40 41 |
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 35 def avg(field) total = count { |doc| !doc.send(field).nil? } return nil unless total > 0 total = total.to_f if total.is_a?(Integer) sum(field) / total end |
#max(field = nil) ⇒ Numeric | Document
Deprecated.
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.
61 62 63 64 65 |
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 61 def max(field = nil) return super() if block_given? aggregate_by(field, :max) end |
#min(field = nil) ⇒ Numeric | Document
Deprecated.
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.
85 86 87 88 89 |
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 85 def min(field = nil) return super() if block_given? aggregate_by(field, :min) end |
#sum(field = nil) ⇒ Numeric
Deprecated.
Get the sum value of the provided field. If provided a block, will return the sum in accordance with Ruby’s enumerable API.
105 106 107 108 109 |
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 105 def sum(field = nil) return super() if block_given? aggregate_by(field, :sum) || 0 end |