Class: Mongoid::Association::Embedded::EmbedsMany::Proxy
- Includes:
- Batchable
- Defined in:
- lib/mongoid/association/embedded/embeds_many/proxy.rb
Overview
Instance Attribute Summary
Attributes inherited from Proxy
#_association, #_base, #_target
Class Method Summary collapse
-
.embedded? ⇒ true
Returns true if the association is an embedded one.
-
.foreign_key_suffix ⇒ nil
Returns the suffix of the foreign key field, either “_id” or “_ids”.
Instance Method Summary collapse
-
#<<(*args) ⇒ Object
(also: #push)
Appends a document or array of documents to the association.
-
#as_document ⇒ Array<Hash>
Get this association as as its representation in the database.
-
#build(attributes = {}, type = nil) {|doc| ... } ⇒ Document
(also: #new)
Builds a new document in the association and appends it to the target.
-
#clear ⇒ self
Clear the association.
-
#concat(docs) ⇒ Array<Document>
Appends an array of documents to the association.
-
#count ⇒ Integer
Returns a count of the number of documents in the association that have actually been persisted to the database.
-
#delete(document) ⇒ Document?
Delete the supplied document from the target.
-
#delete_all(conditions = {}) ⇒ Integer
Delete all the documents in the association without running callbacks.
-
#delete_if ⇒ Many, Enumerator
Delete all the documents for which the provided block returns true.
-
#destroy_all(conditions = {}) ⇒ Integer
Destroy all the documents in the association whilst running callbacks.
-
#exists? ⇒ true, false
Determine if any documents in this association exist in the database.
-
#find(*args) ⇒ Array<Document>, Document
Finds a document in this association through several different methods.
-
#in_memory ⇒ Array<Document>
Get all the documents in the association that are loaded into memory.
-
#initialize(base, target, association) ⇒ Many
constructor
Instantiate a new embeds_many association.
-
#pop(count = nil) ⇒ Document+
Pop documents off the association.
-
#shift(count = nil) ⇒ Document+
Shift documents off the association.
-
#substitute(docs) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation.
-
#unscoped ⇒ Criteria
Return the association with all previous scoping removed.
Methods included from Batchable
#batch_clear, #batch_insert, #batch_remove, #batch_replace
Methods included from Positional
Methods inherited from Many
#blank?, #create, #create!, #find_or_create_by, #find_or_create_by!, #find_or_initialize_by, #nil?, #respond_to?, #scoped, #serializable_hash
Methods inherited from Proxy
apply_ordering, #extend_proxies, #init, #klass, #reset_unloaded, #substitutable
Methods included from Marshalable
Constructor Details
#initialize(base, target, association) ⇒ Many
Instantiate a new embeds_many association.
229 230 231 232 233 234 235 236 237 238 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 229 def initialize(base, target, association) init(base, target, association) do _target.each_with_index do |doc, index| integrate(doc) doc._index = index end @_unscoped = _target.dup @_target = scope(_target) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Criteria, Object (private)
If the target array does not respond to the supplied method then try to find a named scope or criteria on the class and send the call there.
If the method exists on the array, use the default proxy behavior.
418 419 420 421 422 423 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 418 ruby2_keywords def method_missing(name, *args, &block) return super if _target.respond_to?(name) klass.send(:with_scope, criteria) do criteria.public_send(name, *args, &block) end end |
Class Method Details
.embedded? ⇒ true
Returns true if the association is an embedded one. In this case always true.
535 536 537 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 535 def true end |
.foreign_key_suffix ⇒ nil
Returns the suffix of the foreign key field, either “_id” or “_ids”.
547 548 549 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 547 def foreign_key_suffix nil end |
Instance Method Details
#<<(*args) ⇒ Object Also known as: push
Appends a document or array of documents to the association. Will set the parent and update the index in the process.
24 25 26 27 28 29 30 31 32 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 24 def <<(*args) docs = args.flatten return concat(docs) if docs.size > 1 if doc = docs.first append(doc) doc.save if persistable? && !_assigning? end self end |
#as_document ⇒ Array<Hash>
Get this association as as its representation in the database.
44 45 46 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 44 def as_document as_attributes.collect { |attrs| BSON::Document.new(attrs) } end |
#build(attributes = {}, type = nil) {|doc| ... } ⇒ Document Also known as: new
Builds a new document in the association and appends it to the target. Takes an optional type if you want to specify a subclass.
74 75 76 77 78 79 80 81 82 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 74 def build(attributes = {}, type = nil) doc = Factory.build(type || _association.klass, attributes) append(doc) doc.apply_post_processed_defaults yield(doc) if block_given? doc.run_callbacks(:build) { doc } _base._reset_memoized_children! doc end |
#clear ⇒ self
Clear the association. Will delete the documents from the db if they are already persisted.
93 94 95 96 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 93 def clear batch_clear(_target.dup) self end |
#concat(docs) ⇒ Array<Document>
Appends an array of documents to the association. Performs a batch insert of the documents instead of persisting one at a time.
59 60 61 62 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 59 def concat(docs) batch_insert(docs) unless docs.empty? self end |
#count ⇒ Integer
Returns a count of the number of documents in the association that have actually been persisted to the database.
Use #size if you want the total number of documents.
108 109 110 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 108 def count _target.select { |doc| doc.persisted? }.size end |
#delete(document) ⇒ Document?
Delete the supplied document from the target. This method is proxied in order to reindex the array after the operation occurs.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 123 def delete(document) execute_callback :before_remove, document doc = _target.delete_one(document) if doc && !_binding? _unscoped.delete_one(doc) if _assigning? _base.add_atomic_pull(doc) else doc.delete(suppress: true) unbind_one(doc) end end reindex execute_callback :after_remove, document doc end |
#delete_all(conditions = {}) ⇒ Integer
Delete all the documents in the association without running callbacks.
151 152 153 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 151 def delete_all(conditions = {}) remove_all(conditions, :delete) end |
#delete_if ⇒ Many, Enumerator
Delete all the documents for which the provided block returns true.
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 166 def delete_if if block_given? dup_target = _target.dup dup_target.each do |doc| delete(doc) if yield(doc) end self else super end end |
#destroy_all(conditions = {}) ⇒ Integer
Destroy all the documents in the association whilst running callbacks.
189 190 191 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 189 def destroy_all(conditions = {}) remove_all(conditions, :destroy) end |
#exists? ⇒ true, false
Determine if any documents in this association exist in the database.
199 200 201 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 199 def exists? count > 0 end |
#find(*args) ⇒ Array<Document>, Document
Finds a document in this association through several different methods.
215 216 217 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 215 def find(*args) criteria.find(*args) end |
#in_memory ⇒ Array<Document>
Get all the documents in the association that are loaded into memory.
248 249 250 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 248 def in_memory _target end |
#pop(count = nil) ⇒ Document+
Pop documents off the association. This can be a single document or multiples, and will automatically persist the changes.
267 268 269 270 271 272 273 274 275 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 267 def pop(count = nil) if count if docs = _target[_target.size - count, _target.size] docs.each { |doc| delete(doc) } end else delete(_target[-1]) end end |
#shift(count = nil) ⇒ Document+
Shift documents off the association. This can be a single document or multiples, and will automatically persist the changes.
290 291 292 293 294 295 296 297 298 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 290 def shift(count = nil) if count if _target.size > 0 && docs = _target[0, count] docs.each { |doc| delete(doc) } end else delete(_target[0]) end end |
#substitute(docs) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation.
311 312 313 314 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 311 def substitute(docs) batch_replace(docs) self end |
#unscoped ⇒ Criteria
Return the association with all previous scoping removed. This is the exact representation of the docs in the database.
325 326 327 328 329 330 |
# File 'lib/mongoid/association/embedded/embeds_many/proxy.rb', line 325 def unscoped criterion = klass.unscoped criterion. = true criterion.documents = _unscoped.delete_if(&:marked_for_destruction?) criterion end |