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

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

Overview

This class defines the behavior for all associations that are a one-to-one between documents in different collections.

Since:

  • 7.0

Instance Attribute Summary

Attributes inherited from Proxy

#_association, #_base, #_target

Class Method Summary collapse

Instance Method Summary collapse

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

  • base (Document)

    The document this association hangs off of.

  • target (Document)

    The target (child) of the association.

  • association (Association)

    The association metadata.

Since:

  • 7.0



22
23
24
25
26
27
28
29
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_one/proxy.rb', line 22

def initialize(base, target, association)
  init(base, target, association) do
    raise_mixed if klass.embedded? && !klass.cyclic?
    characterize_one(_target)
    bind_one
    _target.save if persistable?
  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

Since:

  • 7.0



99
100
101
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_one/proxy.rb', line 99

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?

Referenced::One.embedded?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0.rc.1



112
113
114
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_one/proxy.rb', line 112

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:

  • 2.0.0.rc.1



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

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.

Since:

  • 2.0.0.rc.1



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_one/proxy.rb', line 56

def substitute(replacement)
  # If the same object currently associated is being assigned,
  # rebind the association and save the target but do not destroy
  # the target.

  unbind_one
  if persistable?
    # TODO can this entire method be skipped if self == replacement?
    if _association.destructive? && self != replacement
      send(_association.dependent)
    else
      save if persisted?
    end
  end
  HasOne::Proxy.new(_base, replacement, _association) if replacement
end