Class: Mongoid::Associations::HasOne
- Includes:
- Proxy
- Defined in:
- lib/mongoid/associations/has_one.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
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(attributes) ⇒ Object
Build a new object for the association.
-
#create(attributes) ⇒ Object
Create a new object for the association and save it.
-
#initialize(document, attributes, 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.
-
#method_missing(name, *args, &block) ⇒ Object
Delegate all missing methods over to the
Document
. -
#nested_build(attributes) ⇒ Object
Used for setting the association via a nested attributes setter on the parent
Document
. -
#valid? ⇒ Boolean
Need to override here for when the underlying document is nil.
Methods included from Proxy
Constructor Details
#initialize(document, attributes, 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 decorated object. options: The association options.
34 35 36 37 38 39 |
# File 'lib/mongoid/associations/has_one.rb', line 34 def initialize(document, attributes, ) @parent, @options, @association_name = document, , .name unless attributes.nil? @document = attributes.assimilate(@parent, @options) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Delegate all missing methods over to the Document
.
42 43 44 45 |
# File 'lib/mongoid/associations/has_one.rb', line 42 def method_missing(name, *args, &block) @document = {}.assimilate(@parent, @options) if @document.nil? @document.send(name, *args, &block) end |
Instance Attribute Details
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
7 8 9 |
# File 'lib/mongoid/associations/has_one.rb', line 7 def association_name @association_name end |
#document ⇒ Object (readonly)
Returns the value of attribute document.
7 8 9 |
# File 'lib/mongoid/associations/has_one.rb', line 7 def document @document end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/mongoid/associations/has_one.rb', line 7 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
7 8 9 |
# File 'lib/mongoid/associations/has_one.rb', line 7 def parent @parent 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.
66 67 68 69 |
# File 'lib/mongoid/associations/has_one.rb', line 66 def instantiate(document, ) attributes = document.attributes[.name] new(document, attributes, ) end |
.macro ⇒ Object
Returns the macro used to create the association.
72 73 74 |
# File 'lib/mongoid/associations/has_one.rb', line 72 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)
88 89 90 |
# File 'lib/mongoid/associations/has_one.rb', line 88 def update(child, parent, ) child.assimilate(parent, ) end |
Instance Method Details
#build(attributes) ⇒ Object
Build a new object for the association.
10 11 12 13 |
# File 'lib/mongoid/associations/has_one.rb', line 10 def build(attributes) @document = attributes.assimilate(@parent, @options) self end |
#create(attributes) ⇒ Object
Create a new object for the association and save it.
16 17 18 19 20 |
# File 'lib/mongoid/associations/has_one.rb', line 16 def create(attributes) build(attributes) @document.save self end |
#nested_build(attributes) ⇒ Object
Used for setting the association via a nested attributes setter on the parent Document
.
49 50 51 |
# File 'lib/mongoid/associations/has_one.rb', line 49 def nested_build(attributes) build(attributes) end |
#valid? ⇒ Boolean
Need to override here for when the underlying document is nil.
54 55 56 |
# File 'lib/mongoid/associations/has_one.rb', line 54 def valid? @document ? @document.valid? : false end |