Class: Mongoid::Contextual::Memory
- Inherits:
-
Object
- Object
- Mongoid::Contextual::Memory
- Includes:
- Enumerable, Association::EagerLoadable, Aggregable::Memory, Queryable, Positional
- Defined in:
- lib/mongoid/contextual/memory.rb
Instance Attribute Summary collapse
-
#documents ⇒ Object
readonly
Returns the value of attribute documents.
- #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
Attributes included from Queryable
#collection, #collection The collection to query against., #criteria, #criteria The criteria for the context., #klass, #klass The klass for the criteria.
Instance Method Summary collapse
-
#==(other) ⇒ true | false
Check if the context is equal to the other object.
-
#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(limit = nil) ⇒ Document
(also: #one, #find_first)
Get the first document in the database for the criteria’s selector.
-
#inc(incs) ⇒ Enumerator
Increment a value on all documents.
-
#initialize(criteria) ⇒ Memory
constructor
Create the new in memory context.
-
#last(limit = nil) ⇒ 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) ⇒ Memory
Limits the number of documents that are returned.
-
#pick(*fields) ⇒ Object+
Pick the field values in memory.
-
#pluck(*fields) ⇒ Array<Object> | Array<Array<Object>>
Pluck the field values in memory.
-
#skip(value) ⇒ Memory
Skips the provided number of documents.
-
#sort(values) ⇒ Memory
Sorts the documents by the provided spec.
-
#take(limit = nil) ⇒ Document
Take the given number of documents from the database.
-
#take! ⇒ Document
Take the given number of documents from the database.
-
#tally(field) ⇒ Hash
Tally the field values in memory.
-
#update(attributes = nil) ⇒ nil | false
Update the first matching document atomically.
-
#update_all(attributes = nil) ⇒ nil | false
Update all the matching documents atomically.
Methods included from Positional
Methods included from Queryable
Methods included from Association::EagerLoadable
#eager_load, #eager_loadable?, #preload
Methods included from Aggregable::Memory
#aggregates, #avg, #max, #min, #sum
Constructor Details
#initialize(criteria) ⇒ Memory
Create the new in memory context.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mongoid/contextual/memory.rb', line 142 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
#documents ⇒ Object (readonly)
Returns the value of attribute documents.
19 20 21 |
# File 'lib/mongoid/contextual/memory.rb', line 19 def documents @documents end |
#matching The in memory documents that match the selector.(The) ⇒ Object (readonly)
19 |
# File 'lib/mongoid/contextual/memory.rb', line 19 attr_reader :documents, :path, :root, :selector |
#path ⇒ Object (readonly)
Returns the value of attribute path.
19 20 21 |
# File 'lib/mongoid/contextual/memory.rb', line 19 def path @path end |
#path The atomic path.(Theatomicpath.) ⇒ Object (readonly)
19 |
# File 'lib/mongoid/contextual/memory.rb', line 19 attr_reader :documents, :path, :root, :selector |
#root ⇒ Object (readonly)
Returns the value of attribute root.
19 20 21 |
# File 'lib/mongoid/contextual/memory.rb', line 19 def root @root end |
#root The root document.(Therootdocument.) ⇒ Object (readonly)
19 |
# File 'lib/mongoid/contextual/memory.rb', line 19 attr_reader :documents, :path, :root, :selector |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
19 20 21 |
# File 'lib/mongoid/contextual/memory.rb', line 19 def selector @selector end |
#selector The root document selector.(Therootdocumentselector.) ⇒ Object (readonly)
19 |
# File 'lib/mongoid/contextual/memory.rb', line 19 attr_reader :documents, :path, :root, :selector |
Instance Method Details
#==(other) ⇒ true | false
Check if the context is equal to the other object.
29 30 31 32 |
# File 'lib/mongoid/contextual/memory.rb', line 29 def ==(other) return false unless other.respond_to?(:entries) entries == other.entries end |
#delete ⇒ nil Also known as: delete_all
Delete all documents in the database that match the selector.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mongoid/contextual/memory.rb', line 40 def delete deleted = count removed = map do |doc| prepare_remove(doc) doc.send(:as_attributes) end unless removed.empty? collection.find(selector).update_one( positionally(selector, "$pullAll" => { path => removed }), session: _session ) end deleted end |
#destroy ⇒ nil Also known as: destroy_all
Destroy all documents in the database that match the selector.
62 63 64 65 66 67 68 69 |
# File 'lib/mongoid/contextual/memory.rb', line 62 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.
80 81 82 83 84 85 86 |
# File 'lib/mongoid/contextual/memory.rb', line 80 def distinct(field) if Mongoid.legacy_pluck_distinct documents.map{ |doc| doc.send(field) }.uniq else pluck(field).uniq end end |
#each ⇒ Enumerator
Iterate over the context. If provided a block, yield to a Mongoid document for each, otherwise return an enum.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/mongoid/contextual/memory.rb', line 97 def each if block_given? documents_for_iteration.each do |doc| yield(doc) end self else to_enum end end |
#exists? ⇒ true | false
Do any documents exist for the context.
114 115 116 |
# File 'lib/mongoid/contextual/memory.rb', line 114 def exists? any? end |
#first(limit = nil) ⇒ Document Also known as: one, find_first
Get the first document in the database for the criteria’s selector.
126 127 128 129 130 131 132 |
# File 'lib/mongoid/contextual/memory.rb', line 126 def first(limit = nil) if limit eager_load(documents.first(limit)) else eager_load([documents.first]).first end end |
#inc(incs) ⇒ Enumerator
Increment a value on all documents.
161 162 163 164 165 |
# File 'lib/mongoid/contextual/memory.rb', line 161 def inc(incs) each do |document| document.inc(incs) end end |
#last(limit = nil) ⇒ Document
Get the last document in the database for the criteria’s selector.
175 176 177 178 179 180 181 |
# File 'lib/mongoid/contextual/memory.rb', line 175 def last(limit = nil) if limit eager_load(documents.last(limit)) else eager_load([documents.last]).first end end |
#length ⇒ Integer Also known as: size
Get the length of matching documents in the context.
222 223 224 |
# File 'lib/mongoid/contextual/memory.rb', line 222 def length documents.length end |
#limit(value) ⇒ Memory
Limits the number of documents that are returned.
235 236 237 238 |
# File 'lib/mongoid/contextual/memory.rb', line 235 def limit(value) self.limiting = value self end |
#pick(*fields) ⇒ Object+
Pick the field values in memory.
266 267 268 269 270 |
# File 'lib/mongoid/contextual/memory.rb', line 266 def pick(*fields) if doc = documents.first pluck_from_doc(doc, *fields) end end |
#pluck(*fields) ⇒ Array<Object> | Array<Array<Object>>
Pluck the field values in memory.
248 249 250 251 252 253 254 255 256 |
# File 'lib/mongoid/contextual/memory.rb', line 248 def pluck(*fields) if Mongoid.legacy_pluck_distinct documents.pluck(*fields) else documents.map do |doc| pluck_from_doc(doc, *fields) end end end |
#skip(value) ⇒ Memory
Skips the provided number of documents.
296 297 298 299 |
# File 'lib/mongoid/contextual/memory.rb', line 296 def skip(value) self.skipping = value self end |
#sort(values) ⇒ Memory
Sorts the documents by the provided spec.
310 311 312 |
# File 'lib/mongoid/contextual/memory.rb', line 310 def sort(values) in_place_sort(values) and self end |
#take(limit = nil) ⇒ Document
Take the given number of documents from the database.
191 192 193 194 195 196 197 |
# File 'lib/mongoid/contextual/memory.rb', line 191 def take(limit = nil) if limit eager_load(documents.take(limit)) else eager_load([documents.first]).first end end |
#take! ⇒ Document
Take the given number of documents from the database.
208 209 210 211 212 213 214 |
# File 'lib/mongoid/contextual/memory.rb', line 208 def take! if documents.empty? raise Errors::DocumentNotFound.new(klass, nil, nil) else eager_load([documents.first]).first end end |
#tally(field) ⇒ Hash
Tally the field values in memory.
280 281 282 283 284 285 286 |
# File 'lib/mongoid/contextual/memory.rb', line 280 def tally(field) return documents.each_with_object({}) do |d, acc| v = retrieve_value_at_path(d, field) acc[v] ||= 0 acc[v] += 1 end end |
#update(attributes = nil) ⇒ nil | false
Update the first matching document atomically.
322 323 324 |
# File 'lib/mongoid/contextual/memory.rb', line 322 def update(attributes = nil) update_documents(attributes, [ first ]) end |
#update_all(attributes = nil) ⇒ nil | false
Update all the matching documents atomically.
334 335 336 |
# File 'lib/mongoid/contextual/memory.rb', line 334 def update_all(attributes = nil) update_documents(attributes, entries) end |