Class: Mongoid::Association::Referenced::HasOne::Proxy
- Extended by:
- ClassMethods
- Defined in:
- lib/mongoid/association/referenced/has_one/proxy.rb
Overview
Transparent proxy for has_one associations. An instance of this class is returned when calling the association getter method on the subject document. This class inherits from Mongoid::Association::Proxy and forwards most of its methods to the target of the association, i.e. the document on the opposite-side collection which must be loaded.
Defined Under Namespace
Modules: ClassMethods
Constant Summary
Constants inherited from Proxy
Instance Attribute Summary
Attributes inherited from Proxy
#_association, #_base, #_target
Instance Method Summary collapse
-
#initialize(base, target, association) ⇒ Proxy
constructor
Instantiate a new references_one association.
-
#nullify ⇒ Object
Removes the association between the base document and the target document by deleting the foreign key and the reference, orphaning the target document in the process.
-
#substitute(replacement) ⇒ One
Substitutes the supplied target document for the existing document in the association.
Methods included from ClassMethods
Methods inherited from One
#__evolve_object_id__, #clear, #in_memory, #respond_to?
Methods inherited from Proxy
apply_ordering, #extend_proxies, #klass, #reset_unloaded, #substitutable
Methods included from Marshalable
Constructor Details
#initialize(base, target, association) ⇒ Proxy
Instantiate a new references_one association. Will set the foreign key and the base on the inverse object.
43 44 45 46 47 48 49 50 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 43 def initialize(base, target, association) super do raise_mixed if klass. && !klass.cyclic? characterize_one(_target) bind_one _target.save if persistable? end end |
Instance Method Details
#nullify ⇒ Object
Removes the association between the base document and the target document by deleting the foreign key and the reference, orphaning the target document in the process.
58 59 60 61 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 58 def nullify unbind_one _target.save end |
#substitute(replacement) ⇒ One
Substitutes the supplied target document for the existing document in the association. If the new target is nil, perform the necessary deletion.
73 74 75 76 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 73 def substitute(replacement) prepare_for_replacement if self != replacement HasOne::Proxy.new(_base, replacement, _association) if replacement end |