Class: Mongoid::Contexts::Enumerable
- Includes:
- 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 ⇒ Hash
Return aggregation counts of the grouped documents.
-
#avg(field) ⇒ Numeric
Get the average value for the supplied field.
-
#count ⇒ Integer
(also: #length, #size)
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) ⇒ Array<String>
Gets an array of distinct values for the supplied field across the entire array or the susbset given the criteria.
-
#execute ⇒ Array<Document>
Enumerable implementation of execute.
-
#group ⇒ Hash
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) ⇒ Numeric
Get the largest value for the field in all the documents.
-
#min(field) ⇒ Numeric
Get the smallest value for the field in all the documents.
-
#shift ⇒ Document
Get one document and tell the criteria to skip this record on successive calls.
-
#sum(field) ⇒ Numeric
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.
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.
127 128 129 |
# File 'lib/mongoid/contexts/enumerable.rb', line 127 def initialize(criteria) @criteria = criteria end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
9 10 11 |
# File 'lib/mongoid/contexts/enumerable.rb', line 9 def collection @collection end |
#criteria ⇒ Object
Returns the value of attribute criteria.
9 10 11 |
# File 'lib/mongoid/contexts/enumerable.rb', line 9 def criteria @criteria end |
Instance Method Details
#aggregate ⇒ Hash
Return aggregation counts of the grouped documents. This will count by the first field provided in the fields array.
21 22 23 24 25 |
# File 'lib/mongoid/contexts/enumerable.rb', line 21 def aggregate {}.tap do |counts| group.each_pair { |key, value| counts[key] = value.size } end end |
#avg(field) ⇒ Numeric
Get the average value for the supplied field.
33 34 35 36 |
# File 'lib/mongoid/contexts/enumerable.rb', line 33 def avg(field) total = sum(field) total ? (total.to_f / count) : nil end |
#count ⇒ Integer Also known as: length, size
Gets the number of documents in the array. Delegates to size.
44 45 46 |
# File 'lib/mongoid/contexts/enumerable.rb', line 44 def count @count ||= execute.size end |
#delete_all ⇒ Integer Also known as: delete
Delete all the documents in the database matching the selector.
58 59 60 61 62 63 64 65 |
# File 'lib/mongoid/contexts/enumerable.rb', line 58 def delete_all atomically(:$pull) do set_collection count.tap do filter.each { |doc| doc.delete } end end end |
#destroy_all ⇒ Integer Also known as: destroy
Destroy all the documents in the database matching the selector.
76 77 78 79 80 81 82 83 |
# File 'lib/mongoid/contexts/enumerable.rb', line 76 def destroy_all atomically(:$pull) do set_collection count.tap do filter.each { |doc| doc.destroy } end end end |
#distinct(field) ⇒ Array<String>
Gets an array of distinct values for the supplied field across the entire array or the susbset given the criteria.
93 94 95 |
# File 'lib/mongoid/contexts/enumerable.rb', line 93 def distinct(field) execute.collect { |doc| doc.send(field) }.uniq end |
#execute ⇒ Array<Document>
Enumerable implementation of execute. Returns matching documents for the selector, and adds options if supplied.
104 105 106 |
# File 'lib/mongoid/contexts/enumerable.rb', line 104 def execute limit(sort(filter)) || [] end |
#group ⇒ Hash
Groups the documents by the first field supplied in the field options.
114 115 116 117 |
# File 'lib/mongoid/contexts/enumerable.rb', line 114 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.
136 137 138 |
# File 'lib/mongoid/contexts/enumerable.rb', line 136 def iterate(&block) execute.each(&block) end |
#max(field) ⇒ Numeric
Get the largest value for the field in all the documents.
146 147 148 |
# File 'lib/mongoid/contexts/enumerable.rb', line 146 def max(field) determine(field, :>=) end |
#min(field) ⇒ Numeric
Get the smallest value for the field in all the documents.
156 157 158 |
# File 'lib/mongoid/contexts/enumerable.rb', line 156 def min(field) determine(field, :<=) end |
#shift ⇒ Document
Get one document and tell the criteria to skip this record on successive calls.
175 176 177 178 179 |
# File 'lib/mongoid/contexts/enumerable.rb', line 175 def shift first.tap do |document| self.criteria = criteria.skip(([:skip] || 0) + 1) end end |
#sum(field) ⇒ Numeric
Get the sum of the field values for all the documents.
187 188 189 190 191 192 |
# File 'lib/mongoid/contexts/enumerable.rb', line 187 def sum(field) sum = execute.inject(nil) do |memo, doc| value = doc.send(field) || 0 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.
204 205 206 207 208 |
# File 'lib/mongoid/contexts/enumerable.rb', line 204 def update_all(attributes = nil) iterate do |doc| doc.update_attributes(attributes || {}) end end |