Class: Mongoid::Association::Referenced::BelongsTo::Proxy

Inherits:
One
  • Object
show all
Includes:
Evolvable
Defined in:
lib/mongoid/association/referenced/belongs_to/proxy.rb

Overview

Transparent proxy for belong_to 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

Constants inherited from Proxy

Proxy::KEEPER_METHODS

Instance Attribute Summary

Attributes inherited from Proxy

#_association, #_base, #_target

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Evolvable

#__evolve_object_id__

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 belongs_to association proxy.

Examples:

Create the new proxy.

Association::BelongsTo::Proxy.new(game, person, association)

Parameters:



26
27
28
29
30
31
# File 'lib/mongoid/association/referenced/belongs_to/proxy.rb', line 26

def initialize(base, target, association)
  super do
    characterize_one(_target)
    bind_one
  end
end

Class Method Details

.eager_loader(association, docs) ⇒ Mongoid::Association::Referenced::BelongsTo::Eager

Get the Eager object for this type of association.

Examples:

Get the eager loader object

Parameters:

Returns:



110
111
112
# File 'lib/mongoid/association/referenced/belongs_to/proxy.rb', line 110

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

.embedded?false

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

Examples:

Is this association embedded?

Association::BelongsTo::Proxy.embedded?

Returns:

  • (false)

    Always false.



121
122
123
# File 'lib/mongoid/association/referenced/belongs_to/proxy.rb', line 121

def embedded?
  false
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


39
40
41
42
# File 'lib/mongoid/association/referenced/belongs_to/proxy.rb', line 39

def nullify
  unbind_one
  _target.save
end

#substitute(replacement) ⇒ self | nil

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

Examples:

Substitute the association.

name.substitute(new_name)

Parameters:

Returns:

  • (self | nil)

    The association or nil.



53
54
55
56
57
58
59
60
# File 'lib/mongoid/association/referenced/belongs_to/proxy.rb', line 53

def substitute(replacement)
  unbind_one
  return unless replacement

  self._target = normalize(replacement)
  bind_one
  self
end