123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Association::Referenced::HasOne::Proxy

Relationships & Source Files
Namespace Children
Modules:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Mongoid::Association::One
Defined in: lib/mongoid/association/referenced/has_one/proxy.rb

Overview

Transparent proxy for has_one associations. An instance of this class is returned when calling the association getter method on the subject document. This class inherits from Mongoid::Association::Proxy and forwards most of its methods to the target of the association, i.e. the document on the opposite-side collection which must be loaded.

Constant Summary

::Mongoid::Association::Proxy - Inherited

KEEPER_METHODS

Class Attribute Summary

ClassMethods - Extended

embedded?

Returns true if the association is an embedded one.

Class Method Summary

ClassMethods - Extended

::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

  • #nullify

    Removes the association between the base document and the target document by deleting the foreign key and the reference, orphaning the target document in the process.

  • #substitute(replacement) ⇒ One

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

  • #binding ⇒ Binding private

    Instantiate the binding associated with this association.

  • #prepare_for_replacement private

    Takes the necessary steps to prepare for the current document to be replaced by a non-nil substitute.

::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) ⇒ Proxy

Instantiate a new references_one association. Will set the foreign key and the base on the inverse object.

Examples:

Create the new association.

Referenced::One.new(base, target, association)

Parameters:

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 43

def initialize(base, target, association)
  super do
    raise_mixed if klass.embedded? && !klass.cyclic?
    characterize_one(_target)
    bind_one
    _target.save if persistable?
  end
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/referenced/has_one/proxy.rb', line 96

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

Instance Method Details

#bindingBinding (private)

Instantiate the binding associated with this association.

Examples:

Get the binding.

relation.binding([ address ])

Returns:

  • (Binding)

    The binding object.

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 86

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

#nullify

Removes the association between the base document and the target document by deleting the foreign key and the reference, orphaning the target document in the process.

Examples:

Nullify the association.

person.game.nullify
[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 58

def nullify
  unbind_one
  _target.save
end

#prepare_for_replacement (private)

Takes the necessary steps to prepare for the current document to be replaced by a non-nil substitute.

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 102

def prepare_for_replacement
  unbind_one

  return unless persistable?

  if _association.destructive?
    send(_association.dependent)
  elsif persisted?
    save
  end
end

#substitute(replacement) ⇒ One

Substitutes the supplied target document for the existing document in the association. If the new target is nil, perform the necessary deletion.

Examples:

Replace the association.

person.game.substitute(new_game)

Parameters:

Returns:

  • (One)

    The association.

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 73

def substitute(replacement)
  prepare_for_replacement if self != replacement
  HasOne::Proxy.new(_base, replacement, _association) if replacement
end