Class: Mongoid::Association::Referenced::HasMany::Enumerable
- Inherits:
-
Object
- Object
- Mongoid::Association::Referenced::HasMany::Enumerable
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/mongoid/association/referenced/has_many/enumerable.rb
Overview
This class is the wrapper for all referenced associations that have a target that can be a criteria or array of _loaded documents. This handles both cases or a combination of the two.
Instance Attribute Summary collapse
-
#_added ⇒ Object
The three main instance variables are collections of documents.
-
#_added Documents that have been appended.(Documentsthathavebeenappended.) ⇒ Object
The three main instance variables are collections of documents.
-
#_loaded ⇒ Object
The three main instance variables are collections of documents.
-
#_loaded Persisted documents that have been _loaded.(Persisteddocumentsthathavebeen_loaded.) ⇒ Object
The three main instance variables are collections of documents.
-
#_unloaded ⇒ Object
The three main instance variables are collections of documents.
-
#_unloaded A criteria representing persisted docs.(Acriteriarepresentingpersisteddocs.) ⇒ Object
The three main instance variables are collections of documents.
Instance Method Summary collapse
-
#<<(document) ⇒ Document
(also: #push)
Append a document to the enumerable.
-
#==(other) ⇒ true | false
Check if the enumerable is equal to the other object.
-
#===(other) ⇒ true | false
Check equality of the enumerable against the provided object for case statements.
-
#_loaded? ⇒ true | false
Has the enumerable been _loaded? This will be true if the criteria has been executed or we manually load the entire thing.
-
#any?(*args) ⇒ true | false
Returns whether the association has any documents, optionally subject to the provided filters.
-
#as_json(options = {}) ⇒ Hash
Send #as_json to the entries, without encoding.
-
#clear ⇒ Array<Document>
Clears out all the documents in this enumerable.
-
#clone ⇒ Array<Document>
Clones each document in the enumerable.
-
#delete(document) {|doc| ... } ⇒ Document
Delete the supplied document from the enumerable.
-
#delete_if(&block) ⇒ Array<Document>
Deletes every document in the enumerable for where the block returns true.
-
#each ⇒ true
Iterating over this enumerable has to handle a few different scenarios.
-
#empty? ⇒ true | false
Is the enumerable empty? Will determine if the count is zero based on whether or not it is _loaded.
-
#first(limit = nil) ⇒ Document
Get the first document in the enumerable.
-
#in_memory ⇒ Array<Document>
Return all the documents in the enumerable that have been _loaded or _added.
-
#include?(doc) ⇒ true | false
Does the target include the provided document?.
-
#initialize(target, base = nil, association = nil) ⇒ Enumerable
constructor
Initialize the new enumerable either with a criteria or an array.
-
#inspect ⇒ String
Inspection will just inspect the entries for nice array-style printing.
-
#last(limit = nil) ⇒ Document
Get the last document in the enumerable.
-
#marshal_dump ⇒ Array<Object>
Provides the data needed to Marshal.dump an enumerable proxy.
-
#marshal_load(data) ⇒ Array<Object>
Loads the data needed to Marshal.load an enumerable proxy.
-
#reset ⇒ false
Reset the enumerable back to its persisted state.
-
#reset_unloaded(criteria) ⇒ Object
Resets the underlying unloaded criteria object with a new one.
-
#respond_to?(name, include_private = false) ⇒ true | false
Does this enumerable respond to the provided method?.
-
#size ⇒ Integer
(also: #length)
Gets the total size of this enumerable.
-
#to_json(options = {}) ⇒ String
Send #to_json to the entries.
-
#uniq ⇒ Array<Document>
Return all the unique documents in the enumerable.
Constructor Details
#initialize(target, base = nil, association = nil) ⇒ Enumerable
Initialize the new enumerable either with a criteria or an array.
265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 265 def initialize(target, base = nil, association = nil) @_base = base @_association = association if target.is_a?(Criteria) @_added, @executed, @_loaded, @_unloaded = {}, false, {}, target else @_added, @executed = {}, true @_loaded = target.inject({}) do |_target, doc| _target[doc._id] = doc if doc _target end end end |
Instance Attribute Details
#_added ⇒ Object
The three main instance variables are collections of documents.
20 21 22 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 20 def _added @_added end |
#_added Documents that have been appended.(Documentsthathavebeenappended.) ⇒ Object
The three main instance variables are collections of documents.
20 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 20 attr_accessor :_added, :_loaded, :_unloaded |
#_loaded ⇒ Object
The three main instance variables are collections of documents.
20 21 22 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 20 def _loaded @_loaded end |
#_loaded Persisted documents that have been _loaded.(Persisteddocumentsthathavebeen_loaded.) ⇒ Object
The three main instance variables are collections of documents.
20 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 20 attr_accessor :_added, :_loaded, :_unloaded |
#_unloaded ⇒ Object
The three main instance variables are collections of documents.
20 21 22 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 20 def _unloaded @_unloaded end |
#_unloaded A criteria representing persisted docs.(Acriteriarepresentingpersisteddocs.) ⇒ Object
The three main instance variables are collections of documents.
20 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 20 attr_accessor :_added, :_loaded, :_unloaded |
Instance Method Details
#<<(document) ⇒ Document Also known as: push
Append a document to the enumerable.
63 64 65 66 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 63 def <<(document) _added[document._id] = document self end |
#==(other) ⇒ true | false
Check if the enumerable is equal to the other object.
32 33 34 35 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 32 def ==(other) return false unless other.respond_to?(:entries) entries == other.entries end |
#===(other) ⇒ true | false
Check equality of the enumerable against the provided object for case statements.
46 47 48 49 50 51 52 53 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 46 def ===(other) return false unless other.respond_to?(:entries) if Mongoid.legacy_triple_equals other.class == Class ? (Array == other || Enumerable == other) : self == other else entries === other.entries end end |
#_loaded? ⇒ true | false
Has the enumerable been _loaded? This will be true if the criteria has been executed or we manually load the entire thing.
356 357 358 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 356 def _loaded? !!@executed end |
#any?(*args) ⇒ true | false
Returns whether the association has any documents, optionally subject to the provided filters.
This method returns true if the association has any persisted documents and if it has any not yet persisted documents.
If the association is already loaded, this method inspects the loaded documents and does not query the database. If the association is not loaded, the argument-less and block-less version does not load the association; the other versions (that delegate to Enumerable) may or may not load the association completely depending on whether it is iterated to completion.
This method can take a parameter and a block. The behavior with either the paramater or the block is delegated to the standard library Enumerable module.
Note that when Enumerable’s any? method is invoked with both a block and a pattern, it only uses the pattern.
228 229 230 231 232 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 228 def any?(*args) return super if args.any? || block_given? !empty? end |
#as_json(options = {}) ⇒ Hash
Send #as_json to the entries, without encoding.
455 456 457 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 455 def as_json( = {}) entries.as_json() end |
#clear ⇒ Array<Document>
Clears out all the documents in this enumerable. If passed a block it will yield to each document that is in memory.
82 83 84 85 86 87 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 82 def clear if block_given? in_memory { |doc| yield(doc) } end _loaded.clear and _added.clear end |
#clone ⇒ Array<Document>
This loads all documents into memory.
Clones each document in the enumerable.
97 98 99 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 97 def clone collect { |doc| doc.clone } end |
#delete(document) {|doc| ... } ⇒ Document
Delete the supplied document from the enumerable.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 109 def delete(document) doc = (_loaded.delete(document._id) || _added.delete(document._id)) unless doc if _unloaded && _unloaded.where(_id: document._id).exists? yield(document) if block_given? return document end end yield(doc) if block_given? doc end |
#delete_if(&block) ⇒ Array<Document>
This operation loads all documents from the database.
Deletes every document in the enumerable for where the block returns true.
132 133 134 135 136 137 138 139 140 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 132 def delete_if(&block) load_all! deleted = in_memory.select(&block) deleted.each do |doc| _loaded.delete(doc._id) _added.delete(doc._id) end self end |
#each ⇒ true
Iterating over this enumerable has to handle a few different scenarios.
If the enumerable has its criteria _loaded into memory then it yields to all the _loaded docs and all the _added docs.
If the enumerable has not _loaded the criteria then it iterates over the cursor while loading the documents and then iterates over the _added docs.
If no block is passed then it returns an enumerator containing all docs.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 165 def each unless block_given? return to_enum end if _loaded? _loaded.each_pair do |id, doc| document = _added.delete(doc._id) || doc set_base(document) yield(document) end else unloaded_documents.each do |doc| document = _added.delete(doc._id) || _loaded.delete(doc._id) || doc _loaded[document._id] = document set_base(document) yield(document) end end _added.each_pair do |id, doc| yield(doc) end @executed = true end |
#empty? ⇒ true | false
Is the enumerable empty? Will determine if the count is zero based on whether or not it is _loaded.
196 197 198 199 200 201 202 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 196 def empty? if _loaded? in_memory.empty? else _added.empty? && !_unloaded.exists? end end |
#first(limit = nil) ⇒ Document
Automatically adding a sort on _id when no other sort is defined on the criteria has the potential to cause bad performance issues. If you experience unexpected poor performance when using #first or #last, use #take instead. Be aware that #take won’t guarantee order.
Get the first document in the enumerable. Will check the persisted documents first. Does not load the entire enumerable.
249 250 251 252 253 254 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 249 def first(limit = nil) _loaded.try(:values).try(:first) || _added[(ul = _unloaded.try(:first, limit)).try(:_id)] || ul || _added.values.try(:first) end |
#in_memory ⇒ Array<Document>
When passed a block it yields to each document.
Return all the documents in the enumerable that have been _loaded or _added.
312 313 314 315 316 317 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 312 def in_memory docs = (_loaded.values + _added.values) docs.each do |doc| yield(doc) if block_given? end end |
#include?(doc) ⇒ true | false
Does the target include the provided document?
287 288 289 290 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 287 def include?(doc) return super unless _unloaded _unloaded.where(_id: doc._id).exists? || _added.has_key?(doc._id) end |
#inspect ⇒ String
Inspection will just inspect the entries for nice array-style printing.
299 300 301 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 299 def inspect entries.inspect end |
#last(limit = nil) ⇒ Document
Automatically adding a sort on _id when no other sort is defined on the criteria has the potential to cause bad performance issues. If you experience unexpected poor performance when using #first or #last, use #take instead. Be aware that #take won’t guarantee order.
Get the last document in the enumerable. Will check the new documents first. Does not load the entire enumerable.
334 335 336 337 338 339 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 334 def last(limit = nil) _added.values.try(:last) || _loaded.try(:values).try(:last) || _added[(ul = _unloaded.try(:last, limit)).try(:_id)] || ul end |
#marshal_dump ⇒ Array<Object>
Provides the data needed to Marshal.dump an enumerable proxy.
366 367 368 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 366 def marshal_dump [_added, _loaded, _unloaded, @executed] end |
#marshal_load(data) ⇒ Array<Object>
Loads the data needed to Marshal.load an enumerable proxy.
376 377 378 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 376 def marshal_load(data) @_added, @_loaded, @_unloaded, @executed = data end |
#reset ⇒ false
Reset the enumerable back to its persisted state.
386 387 388 389 390 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 386 def reset _loaded.clear _added.clear @executed = false end |
#reset_unloaded(criteria) ⇒ Object
Resets the underlying unloaded criteria object with a new one. Used my HABTM associations to keep the underlying array in sync.
399 400 401 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 399 def reset_unloaded(criteria) @_unloaded = criteria if _unloaded.is_a?(Criteria) end |
#respond_to?(name, include_private = false) ⇒ true | false
Does this enumerable respond to the provided method?
413 414 415 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 413 def respond_to?(name, include_private = false) [].respond_to?(name, include_private) || super end |
#size ⇒ Integer Also known as: length
Gets the total size of this enumerable. This is a combination of all the persisted and unpersisted documents.
424 425 426 427 428 429 430 431 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 424 def size count = (_unloaded ? _unloaded.count : _loaded.count) if count.zero? count + _added.count else count + _added.values.count { |d| d.new_record? } end end |
#to_json(options = {}) ⇒ String
Send #to_json to the entries.
443 444 445 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 443 def to_json( = {}) entries.to_json() end |
#uniq ⇒ Array<Document>
This operation loads all documents from the database.
Return all the unique documents in the enumerable.
467 468 469 |
# File 'lib/mongoid/association/referenced/has_many/enumerable.rb', line 467 def uniq entries.uniq end |