Class: Mongoid::Association::Embedded::EmbeddedIn::Proxy

Inherits:
One
  • Object
show all
Defined in:
lib/mongoid/association/embedded/embedded_in/proxy.rb

Overview

Transparent proxy for embedded_in associations. An instance of this class is returned when calling the association getter method on the child document. This class inherits from Mongoid::Association::Proxy and forwards most of its methods to the target of the association, i.e. the parent document.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from One

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

Constructor Details

#initialize(base, target, association) ⇒ In

Instantiate a new embedded_in association.

Examples:

Create the new association.

Association::Embedded::EmbeddedIn.new(person, address, association)

Parameters:



24
25
26
27
28
29
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 24

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

Class Method Details

.eager_loader(associations, docs) ⇒ Mongoid::Association::Embedded::Eager

Returns the eager loader for this association.

Parameters:

  • associations (Array<Mongoid::Association>)

    The associations to be eager loaded

  • docs (Array<Mongoid::Document>)

    The parent documents that possess the given associations, which ought to be populated by the eager-loaded documents.

  • use_lookup (true | false)

    Whether to use a $lookup aggregation stage to perform the eager load.

Returns:



97
98
99
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 97

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

.embedded?true

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

Examples:

Is this association embedded?

Association::Embedded::EmbeddedIn.embedded?

Returns:

  • (true)

    true.



108
109
110
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 108

def embedded?
  true
end

.path(document) ⇒ Root

Get the path calculator for the supplied document.

Examples:

Get the path calculator.

Proxy.path(document)

Parameters:

  • document (Document)

    The document to calculate on.

Returns:

  • (Root)

    The root atomic path calculator.



120
121
122
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 120

def path(document)
  Mongoid::Atomic::Paths::Root.new(document)
end

Instance Method Details

#substitute(replacement) ⇒ Document | nil

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

Examples:

Substitute the new document.

person.name.substitute(new_name)

Parameters:

  • replacement (Document | Hash)

    A document to replace the target.

Returns:

  • (Document | nil)

    The association or nil.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mongoid/association/embedded/embedded_in/proxy.rb', line 40

def substitute(replacement)
  unbind_one
  unless replacement
    _base.delete if persistable?
    return nil
  end
  _base.new_record = true
  replacement = Factory.build(klass, replacement) if replacement.is_a?(::Hash)
  self._target = replacement
  bind_one
  self
end