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
Overview
Context object used for performing bulk query and persistence operations on documents which have been loaded into application memory. The method interface of this class is consistent with Mongoid::Contextual::Mongo.
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(&block) ⇒ Enumerator
Iterate over the context.
-
#exists?(id_or_conditions = :none) ⇒ true | false
Do any documents exist for the context.
-
#fifth ⇒ Document
Get the fifth document in the database for the criteria's selector.
-
#fifth! ⇒ Document
Get the fifth document in the database for the criteria's selector or raise an error if none is found.
-
#first(limit = nil) ⇒ Document
(also: #one, #find_first)
Get the first document in the database for the criteria's selector.
-
#first! ⇒ Document
Get the first document in the database for the criteria's selector or raise an error if none is found.
-
#fourth ⇒ Document
Get the fourth document in the database for the criteria's selector.
-
#fourth! ⇒ Document
Get the fourth document in the database for the criteria's selector or raise an error if none is found.
-
#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.
-
#last! ⇒ Document
Get the last document in the database for the criteria's selector or raise an error if none is found.
-
#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 | Array<Object>
Pick the field values in memory.
-
#pluck(*fields) ⇒ Array<Object> | Array<Array<Object>>
Pluck the field values in memory.
-
#second ⇒ Document
Get the second document in the database for the criteria's selector.
-
#second! ⇒ Document
Get the second document in the database for the criteria's selector or raise an error if none is found.
-
#second_to_last ⇒ Document
Get the second to last document in the database for the criteria's selector.
-
#second_to_last! ⇒ Document
Get the second to last document in the database for the criteria's selector or raise an error if none is found.
-
#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 or raise an error if none are found.
-
#tally(field) ⇒ Hash
Tally the field values in memory.
-
#third ⇒ Document
Get the third document in the database for the criteria's selector.
-
#third! ⇒ Document
Get the third document in the database for the criteria's selector or raise an error if none is found.
-
#third_to_last ⇒ Document
Get the third to last document in the database for the criteria's selector.
-
#third_to_last! ⇒ Document
Get the third to last document in the database for the criteria's selector or raise an error if none is found.
-
#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
#create_pipeline, #eager_load, #eager_load_with_lookup, #eager_loadable?, #preload, #preload_for_lookup, #switch_local_and_foreign_fields?
Methods included from Aggregable::Memory
#aggregates, #avg, #max, #min, #sum
Constructor Details
#initialize(criteria) ⇒ Memory
Create the new in memory context.
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/mongoid/contextual/memory.rb', line 175 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.
23 24 25 |
# File 'lib/mongoid/contextual/memory.rb', line 23 def documents @documents end |
#matching The in memory documents that match the selector.(The) ⇒ Object (readonly)
23 |
# File 'lib/mongoid/contextual/memory.rb', line 23 attr_reader :documents, :path, :root, :selector |
#path ⇒ Object (readonly)
Returns the value of attribute path.
23 24 25 |
# File 'lib/mongoid/contextual/memory.rb', line 23 def path @path end |
#path The atomic path.(Theatomicpath.) ⇒ Object (readonly)
23 |
# File 'lib/mongoid/contextual/memory.rb', line 23 attr_reader :documents, :path, :root, :selector |
#root ⇒ Object (readonly)
Returns the value of attribute root.
23 24 25 |
# File 'lib/mongoid/contextual/memory.rb', line 23 def root @root end |
#root The root document.(Therootdocument.) ⇒ Object (readonly)
23 |
# File 'lib/mongoid/contextual/memory.rb', line 23 attr_reader :documents, :path, :root, :selector |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
23 24 25 |
# File 'lib/mongoid/contextual/memory.rb', line 23 def selector @selector end |
#selector The root document selector.(Therootdocumentselector.) ⇒ Object (readonly)
23 |
# File 'lib/mongoid/contextual/memory.rb', line 23 attr_reader :documents, :path, :root, :selector |
Instance Method Details
#==(other) ⇒ true | false
Check if the context is equal to the other object.
33 34 35 36 37 |
# File 'lib/mongoid/contextual/memory.rb', line 33 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mongoid/contextual/memory.rb', line 45 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.
67 68 69 70 71 72 73 74 |
# File 'lib/mongoid/contextual/memory.rb', line 67 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.
85 86 87 |
# File 'lib/mongoid/contextual/memory.rb', line 85 def distinct(field) pluck(field).uniq end |
#each(&block) ⇒ Enumerator
Iterate over the context. If provided a block, yield to a Mongoid document for each, otherwise return an enum.
98 99 100 101 102 103 104 105 |
# File 'lib/mongoid/contextual/memory.rb', line 98 def each(&block) if block_given? documents_for_iteration.each(&block) self else to_enum end end |
#exists?(id_or_conditions = :none) ⇒ true | false
Do any documents exist for the context.
123 124 125 126 127 128 129 130 |
# File 'lib/mongoid/contextual/memory.rb', line 123 def exists?(id_or_conditions = :none) case id_or_conditions when :none then any? when nil, false then false when Hash then Memory.new(criteria.where(id_or_conditions)).exists? else Memory.new(criteria.where(_id: id_or_conditions)).exists? end end |
#fifth ⇒ Document
Get the fifth document in the database for the criteria's selector.
456 457 458 |
# File 'lib/mongoid/contextual/memory.rb', line 456 def fifth eager_load([ documents.fifth ]).first end |
#fifth! ⇒ Document
Get the fifth document in the database for the criteria's selector or raise an error if none is found.
470 471 472 |
# File 'lib/mongoid/contextual/memory.rb', line 470 def fifth! fifth || raise_document_not_found_error end |
#first(limit = nil) ⇒ Document Also known as: one, find_first
Get the first document in the database for the criteria's selector.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mongoid/contextual/memory.rb', line 140 def first(limit = nil) use_first = limit.nil? limit ||= 1 if criteria.use_lookup? @criteria = criteria.limit(limit) result = eager_load_with_lookup else result = eager_load(documents.first(limit)) end use_first ? result.first : result end |
#first! ⇒ Document
Get the first document in the database for the criteria's selector or raise an error if none is found.
165 166 167 |
# File 'lib/mongoid/contextual/memory.rb', line 165 def first! first || raise_document_not_found_error end |
#fourth ⇒ Document
Get the fourth document in the database for the criteria's selector.
432 433 434 |
# File 'lib/mongoid/contextual/memory.rb', line 432 def fourth eager_load([ documents.fourth ]).first end |
#fourth! ⇒ Document
Get the fourth document in the database for the criteria's selector or raise an error if none is found.
446 447 448 |
# File 'lib/mongoid/contextual/memory.rb', line 446 def fourth! fourth || raise_document_not_found_error end |
#inc(incs) ⇒ Enumerator
Increment a value on all documents.
194 195 196 197 198 |
# File 'lib/mongoid/contextual/memory.rb', line 194 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.
208 209 210 211 212 213 214 |
# File 'lib/mongoid/contextual/memory.rb', line 208 def last(limit = nil) if limit eager_load(documents.last(limit)) else eager_load([ documents.last ]).first end end |
#last! ⇒ Document
Get the last document in the database for the criteria's selector or raise an error if none is found.
226 227 228 |
# File 'lib/mongoid/contextual/memory.rb', line 226 def last! last || raise_document_not_found_error end |
#length ⇒ Integer Also known as: size
Get the length of matching documents in the context.
236 237 238 |
# File 'lib/mongoid/contextual/memory.rb', line 236 def length documents.length end |
#limit(value) ⇒ Memory
Limits the number of documents that are returned.
249 250 251 252 |
# File 'lib/mongoid/contextual/memory.rb', line 249 def limit(value) self.limiting = value self end |
#pick(*fields) ⇒ Object | Array<Object>
Pick the field values in memory.
276 277 278 279 280 |
# File 'lib/mongoid/contextual/memory.rb', line 276 def pick(*fields) return unless doc = documents.first pluck_from_doc(doc, *fields) end |
#pluck(*fields) ⇒ Array<Object> | Array<Array<Object>>
Pluck the field values in memory.
262 263 264 265 266 |
# File 'lib/mongoid/contextual/memory.rb', line 262 def pluck(*fields) documents.map do |doc| pluck_from_doc(doc, *fields) end end |
#second ⇒ Document
Get the second document in the database for the criteria's selector.
384 385 386 |
# File 'lib/mongoid/contextual/memory.rb', line 384 def second eager_load([ documents.second ]).first end |
#second! ⇒ Document
Get the second document in the database for the criteria's selector or raise an error if none is found.
398 399 400 |
# File 'lib/mongoid/contextual/memory.rb', line 398 def second! second || raise_document_not_found_error end |
#second_to_last ⇒ Document
Get the second to last document in the database for the criteria's selector.
480 481 482 |
# File 'lib/mongoid/contextual/memory.rb', line 480 def second_to_last eager_load([ documents.second_to_last ]).first end |
#second_to_last! ⇒ Document
Get the second to last document in the database for the criteria's selector or raise an error if none is found.
494 495 496 |
# File 'lib/mongoid/contextual/memory.rb', line 494 def second_to_last! second_to_last || raise_document_not_found_error end |
#skip(value) ⇒ Memory
Skips the provided number of documents.
336 337 338 339 |
# File 'lib/mongoid/contextual/memory.rb', line 336 def skip(value) self.skipping = value self end |
#sort(values) ⇒ Memory
Sorts the documents by the provided spec.
350 351 352 |
# File 'lib/mongoid/contextual/memory.rb', line 350 def sort(values) in_place_sort(values) and self end |
#take(limit = nil) ⇒ Document
Take the given number of documents from the database.
306 307 308 309 310 311 312 |
# File 'lib/mongoid/contextual/memory.rb', line 306 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 or raise an error if none are found.
324 325 326 |
# File 'lib/mongoid/contextual/memory.rb', line 324 def take! take || raise_document_not_found_error end |
#tally(field) ⇒ Hash
Tally the field values in memory.
290 291 292 293 294 295 296 |
# File 'lib/mongoid/contextual/memory.rb', line 290 def tally(field) documents.each_with_object({}) do |d, acc| v = retrieve_value_at_path(d, field) acc[v] ||= 0 acc[v] += 1 end end |
#third ⇒ Document
Get the third document in the database for the criteria's selector.
408 409 410 |
# File 'lib/mongoid/contextual/memory.rb', line 408 def third eager_load([ documents.third ]).first end |
#third! ⇒ Document
Get the third document in the database for the criteria's selector or raise an error if none is found.
422 423 424 |
# File 'lib/mongoid/contextual/memory.rb', line 422 def third! third || raise_document_not_found_error end |
#third_to_last ⇒ Document
Get the third to last document in the database for the criteria's selector.
504 505 506 |
# File 'lib/mongoid/contextual/memory.rb', line 504 def third_to_last eager_load([ documents.third_to_last ]).first end |
#third_to_last! ⇒ Document
Get the third to last document in the database for the criteria's selector or raise an error if none is found.
518 519 520 |
# File 'lib/mongoid/contextual/memory.rb', line 518 def third_to_last! third_to_last || raise_document_not_found_error end |
#update(attributes = nil) ⇒ nil | false
Update the first matching document atomically.
362 363 364 |
# File 'lib/mongoid/contextual/memory.rb', line 362 def update(attributes = nil) update_documents(attributes, [ first ]) end |
#update_all(attributes = nil) ⇒ nil | false
Update all the matching documents atomically.
374 375 376 |
# File 'lib/mongoid/contextual/memory.rb', line 374 def update_all(attributes = nil) update_documents(attributes, entries) end |