Class: Mongoid::Contexts::Enumerable
- Includes:
- Paging, Relations::Embedded::Atomic
- Defined in:
- lib/mongoid/contexts/enumerable.rb,
lib/mongoid/contexts/enumerable/sort.rb
Defined Under Namespace
Classes: Sort
Constant Summary
Constants included from Relations::Embedded::Atomic
Relations::Embedded::Atomic::MODIFIERS
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#criteria ⇒ Object
Returns the value of attribute criteria.
Instance Method Summary collapse
-
#aggregate ⇒ Object
Return aggregation counts of the grouped documents.
-
#avg(field) ⇒ Object
Get the average value for the supplied field.
-
#count ⇒ Object
Gets the number of documents in the array.
-
#delete_all ⇒ Integer
(also: #delete)
Delete all the documents in the database matching the selector.
-
#destroy_all ⇒ Integer
(also: #destroy)
Destroy all the documents in the database matching the selector.
-
#distinct(field) ⇒ Object
Gets an array of distinct values for the supplied field across the entire array or the susbset given the criteria.
-
#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.
-
#iterate(&block) ⇒ Object
Iterate over each
Document
in the results. -
#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.
-
#shift ⇒ Object
Get one document and tell the criteria to skip this record on successive calls.
-
#sum(field) ⇒ Object
Get the sum of the field values for all the documents.
-
#update_all(attributes = nil) ⇒ Object
(also: #update)
Very basic update that will perform a simple atomic $set of the attributes provided in the hash.
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:
Mongoid::Contexts::Enumerable.new(criteria)
115 116 117 |
# File 'lib/mongoid/contexts/enumerable.rb', line 115 def initialize(criteria) @criteria = criteria end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
10 11 12 |
# File 'lib/mongoid/contexts/enumerable.rb', line 10 def collection @collection end |
#criteria ⇒ Object
Returns the value of attribute criteria.
10 11 12 |
# File 'lib/mongoid/contexts/enumerable.rb', line 10 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
21 22 23 24 25 |
# File 'lib/mongoid/contexts/enumerable.rb', line 21 def aggregate counts = {} group.each_pair { |key, value| counts[key] = value.size } counts end |
#avg(field) ⇒ Object
Get the average value for the supplied field.
Example:
context.avg(:age)
Returns:
A numeric value that is the average.
36 37 38 39 |
# File 'lib/mongoid/contexts/enumerable.rb', line 36 def avg(field) total = sum(field) total ? (total.to_f / count) : nil end |
#count ⇒ Object
Gets the number of documents in the array. Delegates to size.
42 43 44 |
# File 'lib/mongoid/contexts/enumerable.rb', line 42 def count @count ||= filter.size end |
#delete_all ⇒ Integer Also known as: delete
Delete all the documents in the database matching the selector.
54 55 56 57 58 59 |
# File 'lib/mongoid/contexts/enumerable.rb', line 54 def delete_all atomically(:$pull) do set_collection count.tap { filter.each(&:delete) } end end |
#destroy_all ⇒ Integer Also known as: destroy
Destroy all the documents in the database matching the selector.
70 71 72 73 74 75 |
# File 'lib/mongoid/contexts/enumerable.rb', line 70 def destroy_all atomically(:$pull) do set_collection count.tap { filter.each(&:destroy) } end end |
#distinct(field) ⇒ Object
Gets an array of distinct values for the supplied field across the entire array or the susbset given the criteria.
Example:
context.distinct(:title)
84 85 86 |
# File 'lib/mongoid/contexts/enumerable.rb', line 84 def distinct(field) execute.collect { |doc| doc.send(field) }.uniq 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.
94 95 96 |
# File 'lib/mongoid/contexts/enumerable.rb', line 94 def execute(paginating = false) limit(sort(filter)) || [] 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.
103 104 105 106 |
# File 'lib/mongoid/contexts/enumerable.rb', line 103 def group field = field_list.first execute.group_by { |doc| doc.send(field) } end |
#iterate(&block) ⇒ Object
Iterate over each Document
in the results. This can take an optional block to pass to each argument in the results.
Example:
context.iterate { |doc| p doc }
125 126 127 |
# File 'lib/mongoid/contexts/enumerable.rb', line 125 def iterate(&block) execute.each(&block) end |
#max(field) ⇒ Object
Get the largest value for the field in all the documents.
Returns:
The numerical largest value.
134 135 136 |
# File 'lib/mongoid/contexts/enumerable.rb', line 134 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.
143 144 145 |
# File 'lib/mongoid/contexts/enumerable.rb', line 143 def min(field) determine(field, :<=) end |
#shift ⇒ Object
Get one document and tell the criteria to skip this record on successive calls.
Returns:
The first document in the Array
160 161 162 163 164 |
# File 'lib/mongoid/contexts/enumerable.rb', line 160 def shift first.tap do |document| self.criteria = criteria.skip(([:skip] || 0) + 1) end 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.
171 172 173 174 175 176 |
# File 'lib/mongoid/contexts/enumerable.rb', line 171 def sum(field) sum = execute.inject(nil) do |memo, doc| value = doc.send(field) memo ? memo += value : value end end |
#update_all(attributes = nil) ⇒ Object Also known as: update
Very basic update that will perform a simple atomic $set of the attributes provided in the hash. Can be expanded to later for more robust functionality.
188 189 190 191 192 |
# File 'lib/mongoid/contexts/enumerable.rb', line 188 def update_all(attributes = nil) iterate do |doc| doc.update_attributes(attributes || {}) end end |