Class: Mongoid::Relations::Embedded::One
- Defined in:
- lib/mongoid/relations/embedded/one.rb
Overview
This class defines the behaviour needed for embedded one to one relations.
Instance Attribute Summary
Attributes inherited from Proxy
#base, #loaded, #metadata, #target
Class Method Summary collapse
-
.builder(meta, object) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
-
.embedded? ⇒ true
Returns true if the relation is an embedded one.
-
.macro ⇒ Symbol
Returns the macro for this relation.
-
.nested_builder(metadata, attributes, options) ⇒ Builder
Return the nested builder that is responsible for generating the documents that will be used by this relation.
-
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
Instance Method Summary collapse
-
#bind(options = {}) ⇒ Object
Binds the base object to the inverse of the relation.
-
#initialize(base, target, metadata) ⇒ One
constructor
Instantiate a new embeds_one relation.
-
#unbind(old_target, options = {}) ⇒ Object
Unbinds the base object to the inverse of the relation.
Methods inherited from One
Methods inherited from Proxy
Constructor Details
#initialize(base, target, metadata) ⇒ One
Instantiate a new embeds_one relation.
40 41 42 43 44 45 |
# File 'lib/mongoid/relations/embedded/one.rb', line 40 def initialize(base, target, ) init(base, target, ) do characterize_one(target) target.parentize(base) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Proxy
Class Method Details
.builder(meta, object) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
98 99 100 |
# File 'lib/mongoid/relations/embedded/one.rb', line 98 def builder(, object) Builders::Embedded::One.new(, object) end |
.embedded? ⇒ true
Returns true if the relation is an embedded one. In this case always true.
111 112 113 |
# File 'lib/mongoid/relations/embedded/one.rb', line 111 def true end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
124 125 126 |
# File 'lib/mongoid/relations/embedded/one.rb', line 124 def macro :embeds_one end |
.nested_builder(metadata, attributes, options) ⇒ Builder
Return the nested builder that is responsible for generating the documents that will be used by this relation.
150 151 152 |
# File 'lib/mongoid/relations/embedded/one.rb', line 150 def nested_builder(, attributes, ) Builders::NestedAttributes::One.new(, attributes, ) end |
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
163 164 165 |
# File 'lib/mongoid/relations/embedded/one.rb', line 163 def stores_foreign_key? false end |
Instance Method Details
#bind(options = {}) ⇒ Object
Binds the base object to the inverse of the relation. This is so we are referenced to the actual objects themselves and dont hit the database twice when setting the relations up.
This is called after first creating the relation, or if a new object is set on the relation.
27 28 29 30 |
# File 'lib/mongoid/relations/embedded/one.rb', line 27 def bind( = {}) binding.bind() target.save if base.persisted? && ![:binding] end |
#unbind(old_target, options = {}) ⇒ Object
Unbinds the base object to the inverse of the relation. This occurs when setting a side of the relation to nil.
Will delete the object if necessary.
63 64 65 66 |
# File 'lib/mongoid/relations/embedded/one.rb', line 63 def unbind(old_target, = {}) binding(old_target).unbind() old_target.delete if base.persisted? && !old_target.destroyed? end |