Class: Humanoid::Associations::HasOne
- Includes:
- Proxy
- Defined in:
- lib/humanoid/associations/has_one.rb
Overview
:nodoc:
Class Method Summary collapse
-
.instantiate(document, options) ⇒ Object
Preferred method of instantiating a new
HasOne
, 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) ⇒ HasOne
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 included from Proxy
Constructor Details
#initialize(document, attrs, options) ⇒ HasOne
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.
28 29 30 31 32 |
# File 'lib/humanoid/associations/has_one.rb', line 28 def initialize(document, attrs, ) @parent, @options = document, @target = attrs.assimilate(@parent, @options, attrs.klass) extends() end |
Class Method Details
.instantiate(document, options) ⇒ Object
Preferred method of instantiating a new HasOne
, since nil values will be handled properly.
Options:
document: The parent Document
options: The association options.
Returns:
A new HasOne
association proxy.
60 61 62 63 64 |
# File 'lib/humanoid/associations/has_one.rb', line 60 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.
67 68 69 |
# File 'lib/humanoid/associations/has_one.rb', line 67 def macro :has_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:
HasOne.update({:first_name => "Hank"}, person, options)
Returns:
A new HasOne
association proxy.
87 88 89 90 |
# File 'lib/humanoid/associations/has_one.rb', line 87 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.
8 9 10 |
# File 'lib/humanoid/associations/has_one.rb', line 8 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.
44 45 46 |
# File 'lib/humanoid/associations/has_one.rb', line 44 def nested_build(attributes) build(attributes) end |