Class: Mongoid::Associations::EmbedsOne
- Defined in:
- lib/mongoid/associations/embeds_one.rb
Overview
Represents an association that is embedded in a parent document as a one-to-one relationship.
Instance Attribute Summary
Attributes inherited from Proxy
Class Method Summary collapse
-
.instantiate(document, options) ⇒ Object
Preferred method of instantiating a new
EmbedsOne
, since nil values will be handled properly. -
.macro ⇒ Object
Returns the macro used to create the association.
-
.update(child, parent, options) ⇒ Object
Perform an update of the relationship of the parent and child.
Instance Method Summary collapse
-
#build(attrs = {}, type = nil) ⇒ Object
Build a new object for the association.
-
#initialize(document, attrs, options) ⇒ EmbedsOne
constructor
Creates the new association by finding the attributes in the parent document with its name, and instantiating a new document for it.
-
#nested_build(attributes) ⇒ Object
Used for setting the association via a nested attributes setter on the parent
Document
.
Methods inherited from Proxy
Constructor Details
#initialize(document, attrs, options) ⇒ EmbedsOne
Creates the new association by finding the attributes in the parent document with its name, and instantiating a new document for it.
All method calls on this object will then be delegated to the internal document itself.
Options:
document: The parent Document
attributes: The attributes of the target object. options: The association options.
Returns:
A new HashOne
association proxy.
29 30 31 32 33 |
# File 'lib/mongoid/associations/embeds_one.rb', line 29 def initialize(document, attrs, ) @parent, @options = document, @target = attrs.assimilate(@parent, @options, attrs.klass) extends() 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
Preferred method of instantiating a new EmbedsOne
, since nil values will be handled properly.
Options:
document: The parent Document
options: The association options.
Returns:
A new EmbedsOne
association proxy.
61 62 63 64 65 |
# File 'lib/mongoid/associations/embeds_one.rb', line 61 def instantiate(document, ) attributes = document.raw_attributes[.name] return nil if attributes.blank? new(document, attributes, ) end |
.macro ⇒ Object
Returns the macro used to create the association.
68 69 70 |
# File 'lib/mongoid/associations/embeds_one.rb', line 68 def macro :embeds_one end |
.update(child, parent, options) ⇒ Object
Perform an update of the relationship of the parent and child. This will assimilate the child Document
into the parent’s object graph.
Options:
child: The child Document
or Hash
. parent: The parent Document
to update. options: The association Options
Example:
EmbedsOne.update({:first_name => "Hank"}, person, options)
Returns:
A new EmbedsOne
association proxy.
88 89 90 91 |
# File 'lib/mongoid/associations/embeds_one.rb', line 88 def update(child, parent, ) child.assimilate(parent, ) instantiate(parent, ) end |
Instance Method Details
#build(attrs = {}, type = nil) ⇒ Object
Build a new object for the association.
9 10 11 |
# File 'lib/mongoid/associations/embeds_one.rb', line 9 def build(attrs = {}, type = nil) @target = attrs.assimilate(@parent, @options, type); self end |
#nested_build(attributes) ⇒ Object
Used for setting the association via a nested attributes setter on the parent Document
. Called when using accepts_nested_attributes_for.
Options:
attributes: The attributes for the new association
Returns:
A new target document.
45 46 47 |
# File 'lib/mongoid/associations/embeds_one.rb', line 45 def nested_build(attributes) build(attributes) end |