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

Inherits:
One
  • Object
show all
Extended by:
ClassMethods
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.

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants inherited from Proxy

Proxy::KEEPER_METHODS

Instance Attribute Summary

Attributes inherited from Proxy

#_association, #_base, #_target

Instance Method Summary collapse

Methods included from ClassMethods

eager_loader, embedded?

Methods inherited from One

#__evolve_object_id__, #clear, #in_memory, #respond_to?

Methods inherited from Proxy

apply_ordering, #extend_proxies, #klass, #reset_unloaded, #substitutable

Methods included from Marshalable

#marshal_dump, #marshal_load

Constructor Details

#initialize(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:



43
44
45
46
47
48
49
50
# 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 Method Details

#nullifyObject

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


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

def nullify
  unbind_one
  _target.save
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:

  • replacement (Array<Document>)

    The replacement target.

Returns:

  • (One)

    The association.



73
74
75
76
# 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