Class: Humanoid::Contexts::Enumerable
- Defined in:
- lib/humanoid/contexts/enumerable.rb
Instance Attribute Summary collapse
-
#criteria ⇒ Object
readonly
Returns the value of attribute criteria.
Instance Method Summary collapse
-
#aggregate ⇒ Object
Return aggregation counts of the grouped documents.
-
#count ⇒ Object
Gets the number of documents in the array.
-
#execute(paginating = false) ⇒ Object
Enumerable implementation of execute.
-
#group ⇒ Object
Groups the documents by the first field supplied in the field options.
-
#initialize(criteria) ⇒ Enumerable
constructor
Create the new enumerable context.
-
#max(field) ⇒ Object
Get the largest value for the field in all the documents.
-
#min(field) ⇒ Object
Get the smallest value for the field in all the documents.
-
#sum(field) ⇒ Object
Get the sum of the field values for all the documents.
Methods included from Ids
Methods included from Paging
Constructor Details
#initialize(criteria) ⇒ Enumerable
Create the new enumerable context. This will need the selector and options from a Criteria
and a documents array that is the underlying array of embedded documents from a has many association.
Example:
Humanoid::Contexts::Enumerable.new(criteria)
55 56 57 |
# File 'lib/humanoid/contexts/enumerable.rb', line 55 def initialize(criteria) @criteria = criteria end |
Instance Attribute Details
#criteria ⇒ Object (readonly)
Returns the value of attribute criteria.
6 7 8 |
# File 'lib/humanoid/contexts/enumerable.rb', line 6 def criteria @criteria end |
Instance Method Details
#aggregate ⇒ Object
Return aggregation counts of the grouped documents. This will count by the first field provided in the fields array.
Returns:
A Hash
with field values as keys, count as values
17 18 19 20 21 |
# File 'lib/humanoid/contexts/enumerable.rb', line 17 def aggregate counts = {} group.each_pair { |key, value| counts[key] = value.size } counts end |
#count ⇒ Object
Gets the number of documents in the array. Delegates to size.
24 25 26 |
# File 'lib/humanoid/contexts/enumerable.rb', line 24 def count @count ||= documents.size end |
#execute(paginating = false) ⇒ Object
Enumerable implementation of execute. Returns matching documents for the selector, and adds options if supplied.
Returns:
An Array
of documents that matched the selector.
44 45 46 |
# File 'lib/humanoid/contexts/enumerable.rb', line 44 def execute(paginating = false) limit(documents.select { |document| document.matches?(selector) }) end |
#group ⇒ Object
Groups the documents by the first field supplied in the field options.
Returns:
A Hash
with field values as keys, arrays of documents as values.
33 34 35 36 |
# File 'lib/humanoid/contexts/enumerable.rb', line 33 def group field = [:fields].first documents.group_by { |doc| doc.send(field) } end |
#max(field) ⇒ Object
Get the largest value for the field in all the documents.
Returns:
The numerical largest value.
64 65 66 |
# File 'lib/humanoid/contexts/enumerable.rb', line 64 def max(field) determine(field, :>=) end |
#min(field) ⇒ Object
Get the smallest value for the field in all the documents.
Returns:
The numerical smallest value.
73 74 75 |
# File 'lib/humanoid/contexts/enumerable.rb', line 73 def min(field) determine(field, :<=) end |
#sum(field) ⇒ Object
Get the sum of the field values for all the documents.
Returns:
The numerical sum of all the document field values.
89 90 91 92 93 94 |
# File 'lib/humanoid/contexts/enumerable.rb', line 89 def sum(field) sum = documents.inject(nil) do |memo, doc| value = doc.send(field) memo ? memo += value : value end end |