Class: Mongoid::Association::Embedded::EmbedsOne::Proxy

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

Overview

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

Constant Summary collapse

VALID_OPTIONS =

The valid options when defining this association.

Returns:

  • (Array<Symbol>)

    The allowed options when defining this association.

%i[
  autobuild
  as
  cascade_callbacks
  cyclic
  store_as
].freeze

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 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 embeds_one association.

Examples:

Create the new proxy.

One.new(person, name, association)

Parameters:



33
34
35
36
37
38
39
40
41
42
# File 'lib/mongoid/association/embedded/embeds_one/proxy.rb', line 33

def initialize(base, target, association)
  super do
    characterize_one(_target)
    bind_one
    characterize_one(_target)
    update_attributes_hash(_target)
    _base._reset_memoized_descendants!
    _target.save if persistable?
  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.

Returns:



190
191
192
# File 'lib/mongoid/association/embedded/embeds_one/proxy.rb', line 190

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::EmbedsOne.embedded?

Returns:

  • (true)

    true.



201
202
203
# File 'lib/mongoid/association/embedded/embeds_one/proxy.rb', line 201

def embedded?
  true
end

.path(document) ⇒ Mongoid::Atomic::Paths::Embedded::One

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:



214
215
216
# File 'lib/mongoid/association/embedded/embeds_one/proxy.rb', line 214

def path(document)
  Mongoid::Atomic::Paths::Embedded::One.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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mongoid/association/embedded/embeds_one/proxy.rb', line 53

def substitute(replacement)
  return self if replacement == self

  if _assigning?
    _base.add_atomic_unset(_target) unless replacement
  else
    update_target_when_not_assigning(replacement)
  end

  unbind_one

  return nil if replace_with_nil_document(replacement)

  replace_with(replacement)

  self
end