Class: 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.(Persisteddocumentsthathavebeenloaded.) ⇒ 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.
-
#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) ⇒ 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.
-
#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.
-
#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.
-
#reset ⇒ false
Reset the enumerable back to it’s persisted state.
-
#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.
206 207 208 209 210 211 212 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 206 def initialize(target) if target.is_a?(Criteria) @added, @loaded, @unloaded = [], [], target else @added, @executed, @loaded = [], true, target end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
372 373 374 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 372 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.(Persisteddocumentsthathavebeenloaded.) ⇒ 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 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 46 def <<(document) added.push(document) 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 |
#as_json(options = {}) ⇒ Hash
Send #as_json to the entries, without encoding.
352 353 354 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 352 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.
65 66 67 68 69 70 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 65 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.
82 83 84 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 82 def clone collect { |doc| doc.clone } end |
#delete(document) ⇒ Document
Delete the supplied document from the enumerable.
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 96 def delete(document) (loaded.delete(document) || added.delete(document)).tap do |doc| unless doc if unloaded && unloaded.where(:_id => document.id).exists? yield(document) if block_given? return document end end yield(doc) if block_given? end 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.
121 122 123 124 125 126 127 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 121 def delete_if(&block) load_all! tap do loaded.delete_if(&block) added.delete_if(&block) end 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.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 147 def each if loaded? loaded.each do |doc| yield(doc) end else unloaded.each do |doc| document = added.delete_one(doc) || loaded.delete_one(doc) || doc yield(document) loaded.push(document) end end added.each do |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.
174 175 176 177 178 179 180 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 174 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.
191 192 193 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 191 def first added.first || (loaded? ? loaded.first : unloaded.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.
238 239 240 241 242 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 238 def in_memory (loaded + added).tap do |docs| docs.each { |doc| yield(doc) } if block_given? end end |
#inspect ⇒ String
Inspection will just inspect the entries for nice array-style printing.
223 224 225 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 223 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.
253 254 255 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 253 def last added.last || (loaded? ? loaded.last : unloaded.last) 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.
276 277 278 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 276 def loaded? !!@executed end |
#reset ⇒ false
Reset the enumerable back to it’s persisted state.
288 289 290 291 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 288 def reset loaded.clear and added.clear @executed = false end |
#respond_to?(name, include_private = false) ⇒ true, false
Does this enumerable respond to the provided method?
305 306 307 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 305 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.
318 319 320 321 322 323 324 325 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 318 def size count = (unloaded ? unloaded.count : loaded.count) if count.zero? count + added.count else count + added.count{ |d| d.new_record? } end end |
#to_json(options = {}) ⇒ String
Send #to_json to the entries.
338 339 340 |
# File 'lib/mongoid/relations/targets/enumerable.rb', line 338 def to_json( = {}) entries.to_json() end |