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

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

Overview

This class handles all behaviour for relations that are either one-to-many or one-to-one, where the foreign key is stored on this side of the relation 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 relation hangs off of.

  • target (Document, Array<Document>)

    The target (parent) of the relation.

  • association (Association)

    The association object.

Since:

  • 7.0



23
24
25
26
27
28
# File 'build/mongoid-7.0/lib/mongoid/association/referenced/belongs_to/proxy.rb', line 23

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



115
116
117
# File 'build/mongoid-7.0/lib/mongoid/association/referenced/belongs_to/proxy.rb', line 115

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

.embedded?false

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

Examples:

Is this relation embedded?

Association::BelongsTo::Proxy.embedded?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0.rc.1



128
129
130
# File 'build/mongoid-7.0/lib/mongoid/association/referenced/belongs_to/proxy.rb', line 128

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 relation.

person.game.nullify

Since:

  • 7.0



37
38
39
40
# File 'build/mongoid-7.0/lib/mongoid/association/referenced/belongs_to/proxy.rb', line 37

def nullify
  unbind_one
  _target.save
end

#substitute(replacement) ⇒ self?

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

Examples:

Substitute the relation.

name.substitute(new_name)

Parameters:

Returns:

  • (self, nil)

    The relation or nil.

Since:

  • 2.0.0.rc.1



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

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