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

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

Overview

This class handles all behavior for associations that are either one-to-many or one-to-one, where the foreign key is stored on this side of the association and the reference is to document(s) in another collection.

Since:

  • 7.0

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, #init, #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:

  • base (Document)

    The document this association hangs off of.

  • target (Document, Array<Document>)

    The target (parent) of the association.

  • association (Association)

    The association object.

Since:

  • 7.0



25
26
27
28
29
30
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to/proxy.rb', line 25

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongoid::Association::Proxy

Class Method Details

.eager_loader(association, docs) ⇒ Object

Get the Eager object for this type of association.

Examples:

Get the eager loader object

Parameters:

  • association (Association)

    The association object.

  • docs (Array<Document>)

    The array of documents.

Since:

  • 7.0



117
118
119
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to/proxy.rb', line 117

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.

Since:

  • 2.0.0.rc.1



130
131
132
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to/proxy.rb', line 130

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

Since:

  • 7.0



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

def nullify
  unbind_one
  _target.save
end

#substitute(replacement) ⇒ self?

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.

Since:

  • 2.0.0.rc.1



55
56
57
58
59
60
61
62
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to/proxy.rb', line 55

def substitute(replacement)
  unbind_one
  if replacement
    self._target = normalize(replacement)
    bind_one
    self
  end
end