Class: Mongoid::Relations::Embedded::In
- Defined in:
- lib/mongoid/relations/embedded/in.rb
Overview
This class defines the behaviour necessary to handle relations that are embedded within another relation, either as a single document or multiple documents.
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) ⇒ In
constructor
Instantiate a new embedded_in 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) ⇒ In
Instantiate a new embedded_in relation.
43 44 45 46 47 48 |
# File 'lib/mongoid/relations/embedded/in.rb', line 43 def initialize(base, target, ) init(base, target, ) do characterize_one(target) base.parentize(target) 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.
101 102 103 |
# File 'lib/mongoid/relations/embedded/in.rb', line 101 def builder(, object) Builders::Embedded::In.new(, object) end |
.embedded? ⇒ true
Returns true if the relation is an embedded one. In this case always true.
114 115 116 |
# File 'lib/mongoid/relations/embedded/in.rb', line 114 def true end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
127 128 129 |
# File 'lib/mongoid/relations/embedded/in.rb', line 127 def macro :embedded_in 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.
153 154 155 |
# File 'lib/mongoid/relations/embedded/in.rb', line 153 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.
166 167 168 |
# File 'lib/mongoid/relations/embedded/in.rb', line 166 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.
28 29 30 31 |
# File 'lib/mongoid/relations/embedded/in.rb', line 28 def bind( = {}) binding.bind() base.save if target.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.
66 67 68 69 |
# File 'lib/mongoid/relations/embedded/in.rb', line 66 def unbind(old_target, = {}) binding(old_target).unbind() base.delete if old_target.persisted? && !base.destroyed? end |