Module: Mongoid::Association::Bindable
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
|
|
| Defined in: | lib/mongoid/association/bindable.rb |
Overview
Superclass for all objects that bind associations together.
Instance Attribute Summary
- #_association readonly
- #_base readonly
- #_target readonly
::Mongoid::Threaded::Lifecycle - Included
| #_assigning | Begin the assignment of attributes. |
| #_assigning? | Is the current thread in assigning mode? |
| #_binding | Execute a block in binding mode. |
| #_binding? | Is the current thread in binding mode? |
| #_building | Execute a block in building mode. |
| #_building? | Is the current thread in building mode? |
| #_creating? | Is the current thread in creating mode? |
| #_loading | Execute a block in loading mode. |
| #_loading? | Is the current thread in loading mode? |
Instance Method Summary
-
#binding ⇒ Object
Execute the provided block inside a binding.
-
#initialize(base, target, association)
Create the new binding.
-
#bind_foreign_key(keyed, id)
private
Internal use only
Internal use only
::Setthe id of the related document in the foreign key field on the keyed document. -
#bind_from_relational_parent(doc)
private
Internal use only
Internal use only
Bind the provided document with the base from the parent association.
-
#bind_inverse(doc, inverse)
private
Internal use only
Internal use only
Bind the inverse document to the child document so that the in memory instances are the same.
-
#bind_polymorphic_inverse_type(typed, name)
private
Internal use only
Internal use only
::Setthe type of the related document on the foreign type field, used when associations are polymorphic. -
#bind_polymorphic_type(typed, name)
private
Internal use only
Internal use only
::Setthe type of the related document on the foreign type field, used when associations are polymorphic. -
#check_inverse!(doc)
private
Internal use only
Internal use only
Check if the inverse is properly defined.
- #record_id(_base) private
-
#remove_associated(doc)
private
Remove the associated document from the inverse’s association.
-
#remove_associated_in_to(doc, inverse)
private
Remove the associated document from the inverse’s association.
-
#remove_associated_many(doc, inverse)
private
Remove the associated document from the inverse’s association.
-
#set_base_association ⇒ true | false
private
Internal use only
Internal use only
Ensure that the association on the base is correct, for the cases where we have multiple belongs to definitions and were are setting different parents in memory in order.
-
#try_method(object, method_name, *args) ⇒ Object | nil
private
Convenience method to perform
#trybut return nil if the method argument is nil. -
#unbind_from_relational_parent(doc)
private
Internal use only
Internal use only
Bind the provided document with the base from the parent association.
Instance Attribute Details
#_association (readonly)
[ GitHub ]#_base (readonly)
[ GitHub ]# File 'lib/mongoid/association/bindable.rb', line 9
attr_reader :_base, :_target, :_association
#_target (readonly)
[ GitHub ]# File 'lib/mongoid/association/bindable.rb', line 9
attr_reader :_base, :_target, :_association
Instance Method Details
#bind_foreign_key(keyed, id) (private)
::Set the id of the related document in the foreign key field on the keyed document.
# File 'lib/mongoid/association/bindable.rb', line 119
def bind_foreign_key(keyed, id) return if keyed.frozen? try_method(keyed, _association.foreign_key_setter, id) end
#bind_from_relational_parent(doc) (private)
Bind the provided document with the base from the parent association.
# File 'lib/mongoid/association/bindable.rb', line 181
def bind_from_relational_parent(doc) check_inverse!(doc) remove_associated(doc) bind_foreign_key(doc, record_id(_base)) bind_polymorphic_type(doc, _base.class.name) bind_inverse(doc, _base) end
#bind_inverse(doc, inverse) (private)
Bind the inverse document to the child document so that the in memory instances are the same.
# File 'lib/mongoid/association/bindable.rb', line 167
def bind_inverse(doc, inverse) return unless doc.respond_to?(_association.inverse_setter) && !doc.frozen? try_method(doc, _association.inverse_setter, inverse) end
#bind_polymorphic_inverse_type(typed, name) (private)
::Set the type of the related document on the foreign type field, used when associations are polymorphic.
# File 'lib/mongoid/association/bindable.rb', line 151
def bind_polymorphic_inverse_type(typed, name) return unless _association.inverse_type && !typed.frozen? try_method(typed, _association.inverse_type_setter, name) end
#bind_polymorphic_type(typed, name) (private)
::Set the type of the related document on the foreign type field, used when associations are polymorphic.
# File 'lib/mongoid/association/bindable.rb', line 135
def bind_polymorphic_type(typed, name) return unless _association.type && !typed.frozen? try_method(typed, _association.type_setter, name) end
#binding ⇒ Object
Execute the provided block inside a binding.
# File 'lib/mongoid/association/bindable.rb', line 31
def binding return if _binding? _binding do yield(self) if block_given? end end
#check_inverse!(doc) (private)
Check if the inverse is properly defined.
# File 'lib/mongoid/association/bindable.rb', line 51
def check_inverse!(doc) return if _association.bindable?(doc) raise Errors::InverseNotFound.new( _base.class, _association.name, doc.class, _association.foreign_key ) end
#initialize(base, target, association)
Create the new binding.
# File 'lib/mongoid/association/bindable.rb', line 19
def initialize(base, target, association) @_base, @_target, @_association = base, target, association end
#record_id(_base) (private)
[ GitHub ]# File 'lib/mongoid/association/bindable.rb', line 189
def record_id(_base) _base.__send__(_association.primary_key) end
#remove_associated(doc) (private)
Remove the associated document from the inverse’s association.
# File 'lib/mongoid/association/bindable.rb', line 65
def remove_associated(doc) return unless inverse = _association.inverse(doc) if _association.many? remove_associated_many(doc, inverse) elsif _association.in_to? remove_associated_in_to(doc, inverse) end end
#remove_associated_in_to(doc, inverse) (private)
Remove the associated document from the inverse’s association.
This method removes associated on belongs_to and embedded_in associations.
# File 'lib/mongoid/association/bindable.rb', line 101
def remove_associated_in_to(doc, inverse) # We only want to remove the inverse association when the inverse # document is in memory. return unless associated = doc.ivar(inverse) associated.send(_association.setter, nil) end
#remove_associated_many(doc, inverse) (private)
Remove the associated document from the inverse’s association.
This method removes the associated on *_many relationships.
# File 'lib/mongoid/association/bindable.rb', line 81
def remove_associated_many(doc, inverse) # We only want to remove the inverse association when the inverse # document is in memory. return unless inv = doc.ivar(inverse) # This first condition is needed because when assigning the # embeds_many association using the same embeds_many # association, we delete from the array we are about to assign. if _base != inv && (associated = inv.ivar(_association.name)) associated.delete(doc) end end
#set_base_association ⇒ true | false (private)
Ensure that the association on the base is correct, for the cases where we have multiple belongs to definitions and were are setting different parents in memory in order.
# File 'lib/mongoid/association/bindable.rb', line 203
def set_base_association inverse_association = _association.inverse_association(_target) return unless inverse_association != _association && !inverse_association.nil? _base._association = inverse_association end
#try_method(object, method_name, *args) ⇒ Object | nil (private)
Convenience method to perform #try but return nil if the method argument is nil.
# File 'lib/mongoid/association/bindable.rb', line 239
def try_method(object, method_name, *args) object.try(method_name, *args) if method_name end
#unbind_from_relational_parent(doc) (private)
Bind the provided document with the base from the parent association.
# File 'lib/mongoid/association/bindable.rb', line 218
def unbind_from_relational_parent(doc) check_inverse!(doc) bind_foreign_key(doc, nil) bind_polymorphic_type(doc, nil) bind_inverse(doc, nil) end