Class: Mongoid::Association::Embedded::EmbedsMany::Proxy

Inherits:
Many
  • Object
show all
Includes:
Batchable
Defined in:
build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb

Overview

Since:

  • 7.0

Instance Attribute Summary

Attributes inherited from Proxy

#_association, #_base, #_target

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Batchable

#batch_clear, #batch_insert, #batch_remove, #batch_replace

Methods included from Positional

#positionally

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

#marshal_dump, #marshal_load

Constructor Details

#initialize(base, target, association) ⇒ Many

Instantiate a new embeds_many relation.

Examples:

Create the new relation.

Many.new(person, addresses, association)

Parameters:

  • base (Document)

    The document this relation hangs off of.

  • target (Array<Document>)

    The child documents of the relation.

  • association (Association)

    The association metadata

Since:

  • 7.0



226
227
228
229
230
231
232
233
234
235
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 226

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(name, *args, &block) ⇒ 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.

Parameters:

  • name (Symbol, String)

    The name of the method.

  • args (Array)

    The method args

  • block (Proc)

    Optional block to pass.

Returns:

  • (Criteria, Object)

    A Criteria or return value from the target.

Since:

  • 7.0



415
416
417
418
419
420
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 415

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 relation is an embedded one. In this case always true.

Examples:

Is the relation embedded?

Association::Embedded::EmbedsMany.embedded?

Returns:

  • (true)

    true.

Since:

  • 2.0.0.rc.1



532
533
534
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 532

def embedded?
  true
end

.foreign_key_suffixnil

Returns the suffix of the foreign key field, either “_id” or “_ids”.

Examples:

Get the suffix for the foreign key.

Association::Embedded::EmbedsMany.foreign_key_suffix

Returns:

  • (nil)

    nil.

Since:

  • 3.0.0



544
545
546
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 544

def foreign_key_suffix
  nil
end

Instance Method Details

#<<(*args) ⇒ Object Also known as: push

Appends a document or array of documents to the relation. Will set the parent and update the index in the process.

Examples:

Append a document.

person.addresses << address

Push a document.

person.addresses.push(address)

Parameters:

Since:

  • 7.0



21
22
23
24
25
26
27
28
29
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 21

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_documentArray<Hash>

Get this relation as as its representation in the database.

Examples:

Convert the relation to an attributes hash.

person.addresses.as_document

Returns:

  • (Array<Hash>)

    The relation as stored in the db.

Since:

  • 2.0.0.rc.1



41
42
43
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 41

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 relation and appends it to the target. Takes an optional type if you want to specify a subclass.

Examples:

Build a new document on the relation.

person.people.build(:name => "Bozo")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to build the document with.

  • type (Class) (defaults to: nil)

    Optional class to build the document with.

Yields:

  • (doc)

Returns:

Since:

  • 7.0



71
72
73
74
75
76
77
78
79
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 71

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

#clearself

Clear the relation. Will delete the documents from the db if they are already persisted.

Examples:

Clear the relation.

person.addresses.clear

Returns:

  • (self)

    The empty relation.

Since:

  • 7.0



90
91
92
93
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 90

def clear
  batch_clear(_target.dup)
  self
end

#concat(docs) ⇒ Array<Document>

Appends an array of documents to the relation. Performs a batch insert of the documents instead of persisting one at a time.

Examples:

Concat with other documents.

person.addresses.concat([ address_one, address_two ])

Parameters:

  • docs (Array<Document>)

    The docs to add.

Returns:

Since:

  • 2.4.0



56
57
58
59
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 56

def concat(docs)
  batch_insert(docs) unless docs.empty?
  self
end

#countInteger

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.

Examples:

Get the count of persisted documents.

person.addresses.count

Returns:

  • (Integer)

    The total number of persisted embedded docs, as flagged by the #persisted? method.

Since:

  • 7.0



105
106
107
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 105

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.

Examples:

Delete the document from the relation.

person.addresses.delete(address)

Parameters:

  • document (Document)

    The document to be deleted.

Returns:

  • (Document, nil)

    The deleted document or nil if nothing deleted.

Since:

  • 2.0.0.rc.1



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 120

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.

Examples:

Delete all documents from the relation.

person.addresses.delete_all

Conditionally delete documents from the relation.

person.addresses.delete_all({ :street => "Bond" })

Parameters:

  • conditions (Hash) (defaults to: {})

    Conditions on which documents to delete.

Returns:

  • (Integer)

    The number of documents deleted.

Since:

  • 7.0



148
149
150
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 148

def delete_all(conditions = {})
  remove_all(conditions, :delete)
end

#delete_ifMany, Enumerator

Delete all the documents for which the provided block returns true.

Examples:

Delete the matching documents.

person.addresses.delete_if do |doc|
  doc.state == "GA"
end

Returns:

  • (Many, Enumerator)

    The relation or an enumerator if no block was provided.

Since:

  • 3.1.0



163
164
165
166
167
168
169
170
171
172
173
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 163

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.

Examples:

Destroy all documents from the relation.

person.addresses.destroy_all

Conditionally destroy documents from the relation.

person.addresses.destroy_all({ :street => "Bond" })

Parameters:

  • conditions (Hash) (defaults to: {})

    Conditions on which documents to destroy.

Returns:

  • (Integer)

    The number of documents destroyed.

Since:

  • 7.0



186
187
188
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 186

def destroy_all(conditions = {})
  remove_all(conditions, :destroy)
end

#exists?true, false

Determine if any documents in this relation exist in the database.

Examples:

Are there persisted documents?

person.posts.exists?

Returns:

  • (true, false)

    True is persisted documents exist, false if not.

Since:

  • 7.0



196
197
198
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 196

def exists?
  count > 0
end

#find(*args) ⇒ Array<Document>, Document

Finds a document in this association through several different methods.

Examples:

Find a document by its id.

person.addresses.find(BSON::ObjectId.new)

Find documents for multiple ids.

person.addresses.find([ BSON::ObjectId.new, BSON::ObjectId.new ])

Parameters:

  • args (Array<Object>)

    Various arguments.

Returns:

Since:

  • 7.0



212
213
214
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 212

def find(*args)
  criteria.find(*args)
end

#in_memoryArray<Document>

Get all the documents in the relation that are loaded into memory.

Examples:

Get the in memory documents.

relation.in_memory

Returns:

  • (Array<Document>)

    The documents in memory.

Since:

  • 2.1.0



245
246
247
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 245

def in_memory
  _target
end

#pop(count = nil) ⇒ Document+

Pop documents off the relation. This can be a single document or multiples, and will automatically persist the changes.

Examples:

Pop a single document.

relation.pop

Pop multiple documents.

relation.pop(3)

Parameters:

  • count (Integer) (defaults to: nil)

    The number of documents to pop, or 1 if not provided.

Returns:

Since:

  • 3.0.0



264
265
266
267
268
269
270
271
272
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 264

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 relation. This can be a single document or multiples, and will automatically persist the changes.

Examples:

Shift a single document.

relation.shift

Shift multiple documents.

relation.shift(3)

Parameters:

  • count (Integer) (defaults to: nil)

    The number of documents to shift, or 1 if not provided.

Returns:

Since:

  • 7.0



287
288
289
290
291
292
293
294
295
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 287

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.

Examples:

Substitute the relation’s target.

person.addresses.substitute([ address ])

Parameters:

  • docs (Array<Document>)

    The replacement docs.

Returns:

  • (Many)

    The proxied relation.

Since:

  • 2.0.0.rc.1



308
309
310
311
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 308

def substitute(docs)
  batch_replace(docs)
  self
end

#unscopedCriteria

Return the relation with all previous scoping removed. This is the exact representation of the docs in the database.

Examples:

Get the unscoped documents.

person.addresses.unscoped

Returns:

Since:

  • 2.4.0



322
323
324
325
326
327
# File 'build/mongoid-7.0/lib/mongoid/association/embedded/embeds_many/proxy.rb', line 322

def unscoped
  criterion = klass.unscoped
  criterion.embedded = true
  criterion.documents = _unscoped.delete_if(&:marked_for_destruction?)
  criterion
end