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_or_opts = 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_or_opts = 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) ⇒ Mongo
Limits the number of documents that are returned.
- #pluck(*fields) ⇒ Object
-
#skip(value) ⇒ Mongo
Skips the provided number of documents.
-
#sort(values) ⇒ Mongo
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.
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/mongoid/contextual/memory.rb', line 143 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? count > 0 end |
#first(limit_or_opts = nil) ⇒ Document Also known as: one, find_first
Get the first document in the database for the criteria’s selector.
127 128 129 130 131 132 133 |
# File 'lib/mongoid/contextual/memory.rb', line 127 def first(limit_or_opts = nil) if limit_or_opts.nil? || limit_or_opts.is_a?(Hash) eager_load([documents.first]).first else eager_load(documents.first(limit_or_opts)) end end |
#inc(incs) ⇒ Enumerator
Increment a value on all documents.
162 163 164 165 166 |
# File 'lib/mongoid/contextual/memory.rb', line 162 def inc(incs) each do |document| document.inc(incs) end end |
#last(limit_or_opts = nil) ⇒ Document
Get the last document in the database for the criteria’s selector.
180 181 182 183 184 185 186 |
# File 'lib/mongoid/contextual/memory.rb', line 180 def last(limit_or_opts = nil) if limit_or_opts.nil? || limit_or_opts.is_a?(Hash) eager_load([documents.last]).first else eager_load(documents.last(limit_or_opts)) end end |
#length ⇒ Integer Also known as: size
Get the length of matching documents in the context.
227 228 229 |
# File 'lib/mongoid/contextual/memory.rb', line 227 def length documents.length end |
#limit(value) ⇒ Mongo
Limits the number of documents that are returned.
240 241 242 243 |
# File 'lib/mongoid/contextual/memory.rb', line 240 def limit(value) self.limiting = value self end |
#pluck(*fields) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/mongoid/contextual/memory.rb', line 245 def pluck(*fields) if Mongoid.legacy_pluck_distinct documents.pluck(*fields) else documents.map do |d| if fields.length == 1 retrieve_value_at_path(d, fields.first) else fields.map do |field| retrieve_value_at_path(d, field) end end end end end |
#skip(value) ⇒ Mongo
Skips the provided number of documents.
285 286 287 288 |
# File 'lib/mongoid/contextual/memory.rb', line 285 def skip(value) self.skipping = value self end |
#sort(values) ⇒ Mongo
Sorts the documents by the provided spec.
299 300 301 |
# File 'lib/mongoid/contextual/memory.rb', line 299 def sort(values) in_place_sort(values) and self end |
#take(limit = nil) ⇒ Document
Take the given number of documents from the database.
196 197 198 199 200 201 202 |
# File 'lib/mongoid/contextual/memory.rb', line 196 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.
213 214 215 216 217 218 219 |
# File 'lib/mongoid/contextual/memory.rb', line 213 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.
269 270 271 272 273 274 275 |
# File 'lib/mongoid/contextual/memory.rb', line 269 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.
311 312 313 |
# File 'lib/mongoid/contextual/memory.rb', line 311 def update(attributes = nil) update_documents(attributes, [ first ]) end |
#update_all(attributes = nil) ⇒ nil, false
Update all the matching documents atomically.
323 324 325 |
# File 'lib/mongoid/contextual/memory.rb', line 323 def update_all(attributes = nil) update_documents(attributes, entries) end |