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?(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
#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.
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/mongoid/contextual/memory.rb', line 167 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 |
# File 'lib/mongoid/contextual/memory.rb', line 80 def distinct(field) pluck(field).uniq end |
#each ⇒ Enumerator
Iterate over the context. If provided a block, yield to a Mongoid document for each, otherwise return an enum.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mongoid/contextual/memory.rb', line 93 def each if block_given? documents_for_iteration.each do |doc| yield(doc) end self else to_enum end end |
#exists?(id_or_conditions = :none) ⇒ true | false
Do any documents exist for the context.
120 121 122 123 124 125 126 127 |
# File 'lib/mongoid/contextual/memory.rb', line 120 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.
137 138 139 140 141 142 143 |
# File 'lib/mongoid/contextual/memory.rb', line 137 def first(limit = nil) if limit eager_load(documents.first(limit)) else eager_load([documents.first]).first end end |
#first! ⇒ Document
Get the first document in the database for the criteria’s selector or raise an error if none is found.
157 158 159 |
# File 'lib/mongoid/contextual/memory.rb', line 157 def first! first || raise_document_not_found_error end |
#fourth ⇒ Document
Get the fourth document in the database for the criteria’s selector.
430 431 432 |
# File 'lib/mongoid/contextual/memory.rb', line 430 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.
444 445 446 |
# File 'lib/mongoid/contextual/memory.rb', line 444 def fourth! fourth || raise_document_not_found_error end |
#inc(incs) ⇒ Enumerator
Increment a value on all documents.
186 187 188 189 190 |
# File 'lib/mongoid/contextual/memory.rb', line 186 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.
200 201 202 203 204 205 206 |
# File 'lib/mongoid/contextual/memory.rb', line 200 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.
218 219 220 |
# File 'lib/mongoid/contextual/memory.rb', line 218 def last! last || raise_document_not_found_error end |
#length ⇒ Integer Also known as: size
Get the length of matching documents in the context.
228 229 230 |
# File 'lib/mongoid/contextual/memory.rb', line 228 def length documents.length end |
#limit(value) ⇒ Memory
Limits the number of documents that are returned.
241 242 243 244 |
# File 'lib/mongoid/contextual/memory.rb', line 241 def limit(value) self.limiting = value self end |
#pick(*fields) ⇒ Object | Array<Object>
Pick the field values in memory.
268 269 270 271 272 |
# File 'lib/mongoid/contextual/memory.rb', line 268 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.
254 255 256 257 258 |
# File 'lib/mongoid/contextual/memory.rb', line 254 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.
378 379 380 |
# File 'lib/mongoid/contextual/memory.rb', line 378 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.
392 393 394 |
# File 'lib/mongoid/contextual/memory.rb', line 392 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.
482 483 484 |
# File 'lib/mongoid/contextual/memory.rb', line 482 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.
496 497 498 |
# File 'lib/mongoid/contextual/memory.rb', line 496 def second_to_last! second_to_last || raise_document_not_found_error end |
#skip(value) ⇒ Memory
Skips the provided number of documents.
328 329 330 331 |
# File 'lib/mongoid/contextual/memory.rb', line 328 def skip(value) self.skipping = value self end |
#sort(values) ⇒ Memory
Sorts the documents by the provided spec.
342 343 344 |
# File 'lib/mongoid/contextual/memory.rb', line 342 def sort(values) in_place_sort(values) and self end |
#take(limit = nil) ⇒ Document
Take the given number of documents from the database.
298 299 300 301 302 303 304 |
# File 'lib/mongoid/contextual/memory.rb', line 298 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.
316 317 318 |
# File 'lib/mongoid/contextual/memory.rb', line 316 def take! take || raise_document_not_found_error end |
#tally(field) ⇒ Hash
Tally the field values in memory.
282 283 284 285 286 287 288 |
# File 'lib/mongoid/contextual/memory.rb', line 282 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 |
#third ⇒ Document
Get the third document in the database for the criteria’s selector.
404 405 406 |
# File 'lib/mongoid/contextual/memory.rb', line 404 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.
418 419 420 |
# File 'lib/mongoid/contextual/memory.rb', line 418 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.
508 509 510 |
# File 'lib/mongoid/contextual/memory.rb', line 508 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.
522 523 524 |
# File 'lib/mongoid/contextual/memory.rb', line 522 def third_to_last! third_to_last || raise_document_not_found_error end |
#update(attributes = nil) ⇒ nil | false
Update the first matching document atomically.
354 355 356 |
# File 'lib/mongoid/contextual/memory.rb', line 354 def update(attributes = nil) update_documents(attributes, [ first ]) end |
#update_all(attributes = nil) ⇒ nil | false
Update all the matching documents atomically.
366 367 368 |
# File 'lib/mongoid/contextual/memory.rb', line 366 def update_all(attributes = nil) update_documents(attributes, entries) end |