Class: Mongoid::Contextual::Memory
- Inherits:
-
Object
- Object
- Mongoid::Contextual::Memory
- Includes:
- Enumerable, Aggregable::Memory
- Defined in:
- lib/mongoid/contextual/memory.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
- #collection The root collection.(Therootcollection.) ⇒ Object readonly
-
#criteria ⇒ Object
readonly
Returns the value of attribute criteria.
- #criteria The criteria for the context.(Thecriteria) ⇒ Object readonly
-
#documents ⇒ Object
readonly
Returns the value of attribute documents.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
- #klass The criteria class.(Thecriteria) ⇒ Object readonly
- #matching The in memory documents that match the selector.(The) ⇒ Object readonly
-
#path ⇒ Object
readonly
Returns the value of attribute path.
- #path The atomic path.(Theatomicpath.) ⇒ Object readonly
-
#root ⇒ Object
readonly
Returns the value of attribute root.
- #root The root document.(Therootdocument.) ⇒ Object readonly
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
- #selector The root document selector.(Therootdocumentselector.) ⇒ Object readonly
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Check if the context is equal to the other object.
-
#blank? ⇒ true, false
(also: #empty?)
Is the enumerable of matching documents empty?.
-
#delete ⇒ nil
(also: #delete_all)
Delete all documents in the database that match the selector.
-
#destroy ⇒ nil
(also: #destroy_all)
Destroy all documents in the database that match the selector.
-
#distinct(field) ⇒ Array<Object>
Get the distinct values in the db for the provided field.
-
#each ⇒ Enumerator
Iterate over the context.
-
#exists? ⇒ true, false
Do any documents exist for the context.
-
#first ⇒ Document
(also: #one)
Get the first document in the database for the criteria’s selector.
-
#initialize(criteria) ⇒ Memory
constructor
Create the new in memory context.
-
#last ⇒ Document
Get the last document in the database for the criteria’s selector.
-
#length ⇒ Integer
(also: #size)
Get the length of matching documents in the context.
-
#limit(value) ⇒ Mongo
Limits the number of documents that are returned.
-
#skip(value) ⇒ Mongo
Skips the provided number of documents.
-
#sort(values) ⇒ Mongo
Sorts the documents by the provided spec.
-
#update(attributes = nil) ⇒ nil, false
(also: #update_all)
Update all the matching documents atomically.
Methods included from Aggregable::Memory
Constructor Details
#initialize(criteria) ⇒ Memory
Create the new in memory context.
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/mongoid/contextual/memory.rb', line 161 def initialize(criteria) @criteria, @klass = criteria, criteria.klass @documents = criteria.documents.select do |doc| @root ||= doc._root @collection ||= root.collection doc.matches?(criteria.selector) end apply_sorting end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
17 18 19 |
# File 'lib/mongoid/contextual/memory.rb', line 17 def collection @collection end |
#collection The root collection.(Therootcollection.) ⇒ Object (readonly)
17 18 19 20 21 22 23 24 |
# File 'lib/mongoid/contextual/memory.rb', line 17 attr_reader \ :collection, :criteria, :documents, :klass, :path, :root, :selector |
#criteria ⇒ Object (readonly)
Returns the value of attribute criteria.
17 18 19 |
# File 'lib/mongoid/contextual/memory.rb', line 17 def criteria @criteria end |
#criteria The criteria for the context.(Thecriteria) ⇒ Object (readonly)
17 18 19 20 21 22 23 24 |
# File 'lib/mongoid/contextual/memory.rb', line 17 attr_reader \ :collection, :criteria, :documents, :klass, :path, :root, :selector |
#documents ⇒ Object (readonly)
Returns the value of attribute documents.
17 18 19 |
# File 'lib/mongoid/contextual/memory.rb', line 17 def documents @documents end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
17 18 19 |
# File 'lib/mongoid/contextual/memory.rb', line 17 def klass @klass end |
#klass The criteria class.(Thecriteria) ⇒ Object (readonly)
17 18 19 20 21 22 23 24 |
# File 'lib/mongoid/contextual/memory.rb', line 17 attr_reader \ :collection, :criteria, :documents, :klass, :path, :root, :selector |
#matching The in memory documents that match the selector.(The) ⇒ Object (readonly)
17 18 19 20 21 22 23 24 |
# File 'lib/mongoid/contextual/memory.rb', line 17 attr_reader \ :collection, :criteria, :documents, :klass, :path, :root, :selector |
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'lib/mongoid/contextual/memory.rb', line 17 def path @path end |
#path The atomic path.(Theatomicpath.) ⇒ Object (readonly)
17 18 19 20 21 22 23 24 |
# File 'lib/mongoid/contextual/memory.rb', line 17 attr_reader \ :collection, :criteria, :documents, :klass, :path, :root, :selector |
#root ⇒ Object (readonly)
Returns the value of attribute root.
17 18 19 |
# File 'lib/mongoid/contextual/memory.rb', line 17 def root @root end |
#root The root document.(Therootdocument.) ⇒ Object (readonly)
17 18 19 20 21 22 23 24 |
# File 'lib/mongoid/contextual/memory.rb', line 17 attr_reader \ :collection, :criteria, :documents, :klass, :path, :root, :selector |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
17 18 19 |
# File 'lib/mongoid/contextual/memory.rb', line 17 def selector @selector end |
#selector The root document selector.(Therootdocumentselector.) ⇒ Object (readonly)
17 18 19 20 21 22 23 24 |
# File 'lib/mongoid/contextual/memory.rb', line 17 attr_reader \ :collection, :criteria, :documents, :klass, :path, :root, :selector |
Instance Method Details
#==(other) ⇒ true, false
Check if the context is equal to the other object.
36 37 38 39 |
# File 'lib/mongoid/contextual/memory.rb', line 36 def ==(other) return false unless other.respond_to?(:entries) entries == other.entries end |
#blank? ⇒ true, false Also known as: empty?
Is the enumerable of matching documents empty?
49 50 51 |
# File 'lib/mongoid/contextual/memory.rb', line 49 def blank? count == 0 end |
#delete ⇒ nil Also known as: delete_all
Delete all documents in the database that match the selector.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mongoid/contextual/memory.rb', line 62 def delete deleted = count removed = map do |doc| prepare_remove(doc) doc.as_document end unless removed.empty? collection.find(selector).update("$pullAll" => { path => removed }) end deleted end |
#destroy ⇒ nil Also known as: destroy_all
Destroy all documents in the database that match the selector.
83 84 85 86 87 88 89 90 |
# File 'lib/mongoid/contextual/memory.rb', line 83 def destroy deleted = count each do |doc| documents.delete_one(doc) doc.destroy end deleted end |
#distinct(field) ⇒ Array<Object>
Get the distinct values in the db for the provided field.
103 104 105 |
# File 'lib/mongoid/contextual/memory.rb', line 103 def distinct(field) documents.map{ |doc| doc.send(field) }.uniq end |
#each ⇒ Enumerator
Iterate over the context. If provided a block, yield to a Mongoid document for each, otherwise return an enum.
118 119 120 121 122 123 124 125 126 |
# File 'lib/mongoid/contextual/memory.rb', line 118 def each if block_given? documents[skipping || 0, limiting || documents.length].each do |doc| yield doc end else to_enum end end |
#exists? ⇒ true, false
Do any documents exist for the context.
136 137 138 |
# File 'lib/mongoid/contextual/memory.rb', line 136 def exists? count > 0 end |
#first ⇒ Document Also known as: one
Get the first document in the database for the criteria’s selector.
148 149 150 |
# File 'lib/mongoid/contextual/memory.rb', line 148 def first documents.first end |
#last ⇒ Document
Get the last document in the database for the criteria’s selector.
180 181 182 |
# File 'lib/mongoid/contextual/memory.rb', line 180 def last documents.last end |
#length ⇒ Integer Also known as: size
Get the length of matching documents in the context.
192 193 194 |
# File 'lib/mongoid/contextual/memory.rb', line 192 def length documents.length end |
#limit(value) ⇒ Mongo
Limits the number of documents that are returned.
207 208 209 210 |
# File 'lib/mongoid/contextual/memory.rb', line 207 def limit(value) self.limiting = value self end |
#skip(value) ⇒ Mongo
Skips the provided number of documents.
222 223 224 225 |
# File 'lib/mongoid/contextual/memory.rb', line 222 def skip(value) self.skipping = value self end |
#sort(values) ⇒ Mongo
Sorts the documents by the provided spec.
238 239 240 |
# File 'lib/mongoid/contextual/memory.rb', line 238 def sort(values) in_place_sort(values) and self end |
#update(attributes = nil) ⇒ nil, false Also known as: update_all
Update all the matching documents atomically.
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/mongoid/contextual/memory.rb', line 252 def update(attributes = nil) return false unless attributes updates = {} each do |doc| @selector ||= root.atomic_selector doc.write_attributes(attributes) updates.merge!(doc.atomic_position => attributes) end collection.find(selector).update("$set" => updates) end |