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.
Constant Summary collapse
- VALID_OPTIONS =
The allowed options when defining this relation.
[ :autobuild, :as, :cascade_callbacks, :cyclic, :store_as ].freeze
Instance Attribute Summary
Attributes inherited from Proxy
Class Method Summary collapse
-
.builder(base, 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.
-
.foreign_key_suffix ⇒ nil
Returns the suffix of the foreign key field, either “_id” or “_ids”.
-
.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.
-
.path(document) ⇒ Mongoid::Atomic::Paths::Embedded::One
Get the path calculator for the supplied document.
-
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
-
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
-
.validation_default ⇒ true, false
Get the default validation setting for the relation.
Instance Method Summary collapse
-
#initialize(base, target, metadata) ⇒ One
constructor
Instantiate a new embeds_one relation.
-
#substitute(replacement) ⇒ Document?
Substitutes the supplied target documents for the existing document in the relation.
Methods inherited from One
#__evolve_object_id__, #clear, #in_memory, #respond_to?
Methods inherited from Proxy
apply_ordering, #extend_proxies, #init, #klass, #reset_unloaded, #substitutable
Methods included from Marshalable
Constructor Details
#initialize(base, target, metadata) ⇒ One
Instantiate a new embeds_one relation.
31 32 33 34 35 36 37 38 39 |
# File 'lib/mongoid/relations/embedded/one.rb', line 31 def initialize(base, target, ) init(base, target, ) do characterize_one(target) bind_one characterize_one(target) base._reset_memoized_children! target.save if persistable? 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(base, meta, object) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
111 112 113 |
# File 'lib/mongoid/relations/embedded/one.rb', line 111 def builder(base, , object) Builders::Embedded::One.new(base, , object) end |
.embedded? ⇒ true
Returns true if the relation is an embedded one. In this case always true.
124 125 126 |
# File 'lib/mongoid/relations/embedded/one.rb', line 124 def true end |
.foreign_key_suffix ⇒ nil
Returns the suffix of the foreign key field, either “_id” or “_ids”.
136 137 138 |
# File 'lib/mongoid/relations/embedded/one.rb', line 136 def foreign_key_suffix nil end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
149 150 151 |
# File 'lib/mongoid/relations/embedded/one.rb', line 149 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.
175 176 177 |
# File 'lib/mongoid/relations/embedded/one.rb', line 175 def nested_builder(, attributes, ) Builders::NestedAttributes::One.new(, attributes, ) end |
.path(document) ⇒ Mongoid::Atomic::Paths::Embedded::One
Get the path calculator for the supplied document.
190 191 192 |
# File 'lib/mongoid/relations/embedded/one.rb', line 190 def path(document) Mongoid::Atomic::Paths::Embedded::One.new(document) end |
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
203 204 205 |
# File 'lib/mongoid/relations/embedded/one.rb', line 203 def stores_foreign_key? false end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
215 216 217 |
# File 'lib/mongoid/relations/embedded/one.rb', line 215 def VALID_OPTIONS end |
.validation_default ⇒ true, false
Get the default validation setting for the relation. Determines if by default a validates associated will occur.
228 229 230 |
# File 'lib/mongoid/relations/embedded/one.rb', line 228 def validation_default true end |
Instance Method Details
#substitute(replacement) ⇒ Document?
Substitutes the supplied target documents for the existing document in the relation.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mongoid/relations/embedded/one.rb', line 52 def substitute(replacement) if replacement != self if _assigning? base.add_atomic_unset(target) unless replacement else target.destroy if persistable? end unbind_one return nil unless replacement replacement = Factory.build(klass, replacement) if replacement.is_a?(::Hash) self.target = replacement bind_one characterize_one(target) target.save if persistable? && !_assigning? end self end |