Class: Mongoid::Associations::EmbeddedIn

Inherits:
Proxy show all
Defined in:
lib/mongoid/associations/embedded_in.rb

Overview

Represents an association that is embedded within another document in the database, either as one or many.

Instance Attribute Summary

Attributes inherited from Proxy

#options, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Proxy

#extends, #method_missing

Constructor Details

#initialize(target, options) ⇒ EmbeddedIn

Creates the new association by setting the internal target as the passed in Document. This should be the parent.

All method calls on this object will then be delegated to the internal document itself.

Options:

target: The parent Document options: The association options



19
20
21
22
# File 'lib/mongoid/associations/embedded_in.rb', line 19

def initialize(target, options)
  @target, @options = target, options
  extends(options)
end

Dynamic Method Handling

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

Class Method Details

.instantiate(document, options) ⇒ Object

Creates the new association by setting the internal document as the passed in Document. This should be the parent.

Options:

document: The parent Document options: The association options



40
41
42
43
# File 'lib/mongoid/associations/embedded_in.rb', line 40

def instantiate(document, options)
  target = document._parent
  target.nil? ? nil : new(target, options)
end

.macroObject

Returns the macro used to create the association.



46
47
48
# File 'lib/mongoid/associations/embedded_in.rb', line 46

def macro
  :embedded_in
end

.update(target, child, options) ⇒ Object

Perform an update of the relationship of the parent and child. This is initialized by setting a parent object as the association on the Document. Will properly set an embeds_one or an embeds_many.

Returns:

A new EmbeddedIn association proxy.



57
58
59
60
61
# File 'lib/mongoid/associations/embedded_in.rb', line 57

def update(target, child, options)
  child.parentize(target, determine_name(target, options))
  child.notify
  instantiate(child, options)
end

Instance Method Details

#find(id) ⇒ Object

Returns the parent document. The id param is present for compatibility with rails, however this could be overwritten in the future.



27
28
29
# File 'lib/mongoid/associations/embedded_in.rb', line 27

def find(id)
  @target
end