Class: Mongoid::Relations::Targets::Enumerable
- Inherits:
-
Object
- Object
- Mongoid::Relations::Targets::Enumerable
- Includes:
- Enumerable
- Defined in:
- lib/mongoid/relations/targets/enumerable.rb
Overview
This class is the wrapper for all relational 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.
-
#_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.
-
#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 ⇒ 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) ⇒ 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 ⇒ 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) ⇒ Enumerable
Initialize the new enumerable either with a criteria or an array.
219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 219 def initialize(target) 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 _target end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
441 442 443 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 441 def method_missing(name, *args, &block) entries.send(name, *args, &block) end |
Instance Attribute Details
#_added ⇒ Object
The three main instance variables are collections of documents.
17 18 19 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17 def _added @_added end |
#_added Documents that have been appended.(Documentsthathavebeenappended.) ⇒ Object
The three main instance variables are collections of documents.
17 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17 attr_accessor :_added, :_loaded, :_unloaded |
#_loaded ⇒ Object
The three main instance variables are collections of documents.
17 18 19 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17 def _loaded @_loaded end |
#_loaded Persisted documents that have been _loaded.(Persisteddocumentsthathavebeen_loaded.) ⇒ Object
The three main instance variables are collections of documents.
17 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17 attr_accessor :_added, :_loaded, :_unloaded |
#_unloaded ⇒ Object
The three main instance variables are collections of documents.
17 18 19 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17 def _unloaded @_unloaded end |
#_unloaded A criteria representing persisted docs.(Acriteriarepresentingpersisteddocs.) ⇒ Object
The three main instance variables are collections of documents.
17 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17 attr_accessor :_added, :_loaded, :_unloaded |
Instance Method Details
#<<(document) ⇒ Document Also known as: push
Append a document to the enumerable.
46 47 48 49 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 46 def <<(document) _added[document.id] = document self end |
#==(other) ⇒ true, false
Check if the enumerable is equal to the other object.
31 32 33 34 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 31 def ==(other) return false unless other.respond_to?(:entries) entries == other.entries 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.
308 309 310 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 308 def _loaded? !!@executed end |
#as_json(options = {}) ⇒ Hash
Send #as_json to the entries, without encoding.
421 422 423 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 421 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.
66 67 68 69 70 71 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 66 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.
83 84 85 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 83 def clone collect { |doc| doc.clone } end |
#delete(document) {|doc| ... } ⇒ Document
Delete the supplied document from the enumerable.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 97 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.
122 123 124 125 126 127 128 129 130 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 122 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.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 157 def each unless block_given? return to_enum end if _loaded? _loaded.each_pair do |id, doc| yield(doc) end else unloaded_documents.each do |doc| document = _added.delete(doc.id) || _loaded.delete(doc.id) || doc _loaded[document.id] = 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.
187 188 189 190 191 192 193 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 187 def empty? if _loaded? in_memory.count == 0 else _unloaded.count + _added.count == 0 end end |
#first ⇒ Document
Get the first document in the enumerable. Will check the persisted documents first. Does not load the entire enumerable.
204 205 206 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 204 def first matching_document(: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.
270 271 272 273 274 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 270 def in_memory docs = (_loaded.values + _added.values) docs.each { |doc| yield(doc) } if block_given? docs end |
#include?(doc) ⇒ true, false
Does the target include the provided document?
241 242 243 244 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 241 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.
255 256 257 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 255 def inspect entries.inspect end |
#last ⇒ Document
Get the last document in the enumerable. Will check the new documents first. Does not load the entire enumerable.
285 286 287 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 285 def last matching_document(:last) end |
#marshal_dump ⇒ Array<Object>
Provides the data needed to Marshal.dump an enumerable proxy.
320 321 322 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 320 def marshal_dump [ _added, _loaded, _unloaded ] end |
#marshal_load(data) ⇒ Array<Object>
Loads the data needed to Marshal.load an enumerable proxy.
332 333 334 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 332 def marshal_load(data) @_added, @_loaded, @_unloaded = data end |
#reset ⇒ false
Reset the enumerable back to its persisted state.
344 345 346 347 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 344 def reset _loaded.clear and _added.clear @executed = false end |
#reset_unloaded(criteria) ⇒ Object
Resets the underlying unloaded criteria object with a new one. Used my HABTM relations to keep the underlying array in sync.
358 359 360 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 358 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?
374 375 376 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 374 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.
387 388 389 390 391 392 393 394 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 387 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.
407 408 409 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 407 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.
435 436 437 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 435 def uniq entries.uniq end |