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(attrs = {}, type = nil) ⇒ Object
Build a new object for the association.
-
#create(attrs = {}, type = nil) ⇒ 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 40 |
# File 'lib/mongoid/associations/has_one.rb', line 34 def initialize(document, attributes, ) @parent, @options, @association_name = document, , .name unless attributes.nil? klass = attributes[:_type] ? attributes[:_type].constantize : nil @document = attributes.assimilate(@parent, @options, klass) 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
.
43 44 45 46 |
# File 'lib/mongoid/associations/has_one.rb', line 43 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.
67 68 69 70 |
# File 'lib/mongoid/associations/has_one.rb', line 67 def instantiate(document, ) attributes = document.attributes[.name] new(document, attributes, ) end |
.macro ⇒ Object
Returns the macro used to create the association.
73 74 75 |
# File 'lib/mongoid/associations/has_one.rb', line 73 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)
89 90 91 |
# File 'lib/mongoid/associations/has_one.rb', line 89 def update(child, parent, ) child.assimilate(parent, ) end |
Instance Method Details
#build(attrs = {}, type = nil) ⇒ Object
Build a new object for the association.
10 11 12 13 |
# File 'lib/mongoid/associations/has_one.rb', line 10 def build(attrs = {}, type = nil) @document = attrs.assimilate(@parent, @options, type) self end |
#create(attrs = {}, type = nil) ⇒ 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(attrs = {}, type = nil) build(attrs, type) @document.save self end |
#nested_build(attributes) ⇒ Object
Used for setting the association via a nested attributes setter on the parent Document
.
50 51 52 |
# File 'lib/mongoid/associations/has_one.rb', line 50 def nested_build(attributes) build(attributes) end |
#valid? ⇒ Boolean
Need to override here for when the underlying document is nil.
55 56 57 |
# File 'lib/mongoid/associations/has_one.rb', line 55 def valid? @document ? @document.valid? : false end |