123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Association::Embedded::EmbeddedIn::Proxy

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Mongoid::Association::One
Defined in: lib/mongoid/association/embedded/embedded_in/proxy.rb

Overview

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.

Constant Summary

::Mongoid::Association::Proxy - Inherited

KEEPER_METHODS

Class Attribute Summary

Class Method Summary

::Mongoid::Association::Proxy - Inherited

.apply_ordering

Apply ordering to the criteria if it was defined on the association.

.new

Sets the target and the association metadata properties.

Instance Attribute Summary

::Mongoid::Association::Proxy - Inherited

#_association,
#_base

Model instance for the base of the association.

#_target

Model instance for one to one associations, or array of model instances for one to many associations, for the target of the association.

::Mongoid::Threaded::Lifecycle - Included

#_assigning

Begin the assignment of attributes.

#_assigning?

Is the current thread in assigning mode?

#_binding

Execute a block in binding mode.

#_binding?

Is the current thread in binding mode?

#_building

Execute a block in building mode.

#_building?

Is the current thread in building mode?

#_creating?

Is the current thread in creating mode?

#_loading

Execute a block in loading mode.

#_loading?

Is the current thread in loading mode?

Instance Method Summary

::Mongoid::Association::One - Inherited

#__evolve_object_id__

Evolve the proxy document into an object id.

#clear

Clear this relation - same as calling #delete on the document.

#in_memory

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

#respond_to?

Since method_missing is overridden we should override this as well.

::Mongoid::Association::Proxy - Inherited

#extend_proxies

Allow extension to be an array and extend each module.

#extend_proxy,
#klass

Get the class from the association, or return nil if no association present.

#reset_unloaded

Resets the criteria inside the association proxy.

#substitutable

The default substitutable object for an association proxy is the clone of the target.

::Mongoid::Association::Marshalable - Included

#marshal_dump

Provides the data needed to Marshal.dump an association proxy.

#marshal_load

Takes the provided data and sets it back on the proxy.

Constructor Details

.new(base, target, association) ⇒ In

Instantiate a new embedded_in association.

Examples:

Create the new association.

Association::Embedded::EmbeddedIn.new(person, address, association)

Parameters:

[ GitHub ]

  
# 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

Class Attribute Details

.embedded?true (readonly)

Returns true if the association is an embedded one. In this case always true.

Examples:

Is this association embedded?

Association::Embedded::EmbeddedIn.embedded?

Returns:

  • (true)

    true.

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 106

def embedded?
  true
end

Class Method Details

.eager_loader(associations, docs) ⇒ Mongoid::Association::Embedded::Eager

Returns the eager loader for this association.

Parameters:

  • associations (Array<Mongoid::Association>)

    The associations to be eager loaded

  • docs (Array<Mongoid::Document>)

    The parent documents that possess the given associations, which ought to be populated by the eager-loaded documents.

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 95

def eager_loader(associations, docs)
  Eager.new(associations, docs)
end

.path(document) ⇒ Root

Get the path calculator for the supplied document.

Examples:

Get the path calculator.

Proxy.path(document)

Parameters:

  • document (Document)

    The document to calculate on.

Returns:

  • (Root)

    The root atomic path calculator.

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 118

def path(document)
  Mongoid::Atomic::Paths::Root.new(document)
end

Instance Attribute Details

#persistable?true | false (readonly, private)

Are we able to persist this association?

Examples:

Can we persist the association?

relation.persistable?

Returns:

  • (true | false)

    If the association is persistable.

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 81

def persistable?
  _target.persisted? && !_binding? && !_building?
end

Instance Method Details

#bindingBinding (private)

Instantiate the binding associated with this association.

Examples:

Get the binding.

binding([ address ])

Returns:

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 61

def binding
  Binding.new(_base, _target, _association)
end

#characterize_one(document) (private)

Characterize the document.

Examples:

::Set the base association.

object.characterize_one(document)

Parameters:

  • document (Document)

    The document to set the association metadata on.

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 71

def characterize_one(document)
  _base._association ||= _association.inverse_association(document)
end

#substitute(replacement) ⇒ Document | nil

Substitutes the supplied target documents for the existing document in the association.

Examples:

Substitute the new document.

person.name.substitute(new_name)

Parameters:

  • replacement (Document | Hash)

    A document to replace the target.

Returns:

  • (Document | nil)

    The association or nil.

[ GitHub ]

  
# 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