Class: MongoDoc::Contexts::Enumerable
- Includes:
- Mongoid::Contexts::Paging
- Defined in:
- lib/mongodoc/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.
-
#id_criteria(params) ⇒ Object
Return documents based on an id search.
-
#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 Mongoid::Contexts::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:
MongoDoc::Contexts::Enumerable.new(criteria)
71 72 73 |
# File 'lib/mongodoc/contexts/enumerable.rb', line 71 def initialize(criteria) @criteria = criteria end |
Instance Attribute Details
#criteria ⇒ Object (readonly)
Returns the value of attribute criteria.
6 7 8 |
# File 'lib/mongodoc/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/mongodoc/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/mongodoc/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/mongodoc/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/mongodoc/contexts/enumerable.rb', line 33 def group field = [:fields].first documents.group_by { |doc| doc.send(field) } end |
#id_criteria(params) ⇒ Object
Return documents based on an id search. Will handle if a single id has been passed or mulitple ids.
Example:
context.id_criteria([1, 2, 3])
Returns:
The single or multiple documents.
58 59 60 61 62 |
# File 'lib/mongodoc/contexts/enumerable.rb', line 58 def id_criteria(params) criteria.id(params) result = params.is_a?(String) ? one : criteria.entries return result end |
#max(field) ⇒ Object
Get the largest value for the field in all the documents.
Returns:
The numerical largest value.
80 81 82 |
# File 'lib/mongodoc/contexts/enumerable.rb', line 80 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.
89 90 91 |
# File 'lib/mongodoc/contexts/enumerable.rb', line 89 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.
105 106 107 108 109 110 |
# File 'lib/mongodoc/contexts/enumerable.rb', line 105 def sum(field) sum = documents.inject(nil) do |memo, doc| value = doc.send(field) memo ? memo += value : value end end |