class Mongoid::Association::Embedded::EmbeddedIn::Proxy
Transparent proxy for embedded_in associations. An instance of this class is returned when calling the association getter method on the child document. This class inherits from Mongoid::Association::Proxy
and forwards most of its methods to the target of the association, i.e. the parent document.
Public Class Methods
Returns the eager loader for this association.
@param [ Array<Mongoid::Association> ] associations The
associations to be eager loaded
@param [ Array<Mongoid::Document> ] docs The parent documents
that possess the given associations, which ought to be populated by the eager-loaded documents.
@return [ Mongoid::Association::Embedded::Eager
]
# File lib/mongoid/association/embedded/embedded_in/proxy.rb, line 95 def eager_loader(associations, docs) Eager.new(associations, docs) end
Returns true if the association is an embedded one. In this case always true.
@example Is this association embedded?
Association::Embedded::EmbeddedIn.embedded?
@return [ true ] true.
# File lib/mongoid/association/embedded/embedded_in/proxy.rb, line 106 def embedded? true end
Instantiate a new embedded_in association.
@example Create the new association.
Association::Embedded::EmbeddedIn.new(person, address, association)
@param [ Document
] base The document the association hangs off of. @param [ Document
] target The target (parent) of the association. @param [ Mongoid::Association::Relatable
] association The association metadata.
@return [ In ] The proxy.
Mongoid::Association::Proxy::new
# File lib/mongoid/association/embedded/embedded_in/proxy.rb, line 24 def initialize(base, target, association) super do characterize_one(_target) bind_one end end
Get the path calculator for the supplied document.
@example Get the path calculator.
Proxy.path(document)
@param [ Document
] document The document to calculate on.
@return [ Root ] The root atomic path calculator.
# File lib/mongoid/association/embedded/embedded_in/proxy.rb, line 118 def path(document) Mongoid::Atomic::Paths::Root.new(document) end
Public Instance Methods
Substitutes the supplied target documents for the existing document in the association.
@example Substitute the new document.
person.name.substitute(new_name)
@param [ Document
| Hash ] replacement A document to replace the target.
@return [ Document
| nil ] The association or nil.
# File lib/mongoid/association/embedded/embedded_in/proxy.rb, line 40 def substitute(replacement) unbind_one unless replacement _base.delete if persistable? return nil end _base.new_record = true replacement = Factory.build(klass, replacement) if replacement.is_a?(::Hash) self._target = replacement bind_one self end
Private Instance Methods
Instantiate the binding associated with this association.
@example Get the binding.
binding([ address ])
@return [ Binding
] A binding object.
# File lib/mongoid/association/embedded/embedded_in/proxy.rb, line 61 def binding Binding.new(_base, _target, _association) end
Characterize the document.
@example Set the base association.
object.characterize_one(document)
@param [ Document
] document The document to set the association metadata on.
# File lib/mongoid/association/embedded/embedded_in/proxy.rb, line 71 def characterize_one(document) _base._association ||= _association.inverse_association(document) end
Are we able to persist this association?
@example Can we persist the association?
relation.persistable?
@return [ true | false ] If the association is persistable.
# File lib/mongoid/association/embedded/embedded_in/proxy.rb, line 81 def persistable? _target.persisted? && !_binding? && !_building? end