123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Association::Embedded::EmbeddedIn::Binding

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/mongoid/association/embedded/embedded_in/binding.rb

Overview

The Binding object for embedded_in associations.

Instance Attribute Summary

::Mongoid::Association::Bindable - Included

::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

  • #bind_one

    Binds the base object to the inverse of the association.

  • #unbind_one

    Unbinds the base object and the inverse, caused by setting the reference to nil.

  • #check_polymorphic_inverses!(doc) private Internal use only Internal use only

    Check for problems with multiple inverse definitions.

::Mongoid::Association::Bindable - Included

#binding

Execute the provided block inside a binding.

#initialize

Create the new binding.

#bind_foreign_key

::Set the id of the related document in the foreign key field on the keyed document.

#bind_from_relational_parent

Bind the provided document with the base from the parent association.

#bind_inverse

Bind the inverse document to the child document so that the in memory instances are the same.

#bind_polymorphic_inverse_type

::Set the type of the related document on the foreign type field, used when associations are polymorphic.

#bind_polymorphic_type

::Set the type of the related document on the foreign type field, used when associations are polymorphic.

#check_inverse!

Check if the inverse is properly defined.

#record_id,
#remove_associated

Remove the associated document from the inverse’s association.

#remove_associated_in_to

Remove the associated document from the inverse’s association.

#remove_associated_many

Remove the associated document from the inverse’s association.

#set_base_association

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

Convenience method to perform #try but return nil if the method argument is nil.

#unbind_from_relational_parent

Bind the provided document with the base from the parent association.

Instance Method Details

#bind_one

Binds the base object to the inverse of the association. This is so we are referenced to the actual objects themselves on both sides.

This case sets the association metadata on the inverse object as well as the document itself.

Examples:

Bind the documents.

name.person.bind(:continue => true)
name.person = Person.new
[ GitHub ]

  
# File 'lib/mongoid/association/embedded/embedded_in/binding.rb', line 22

def bind_one
  binding do
    check_polymorphic_inverses!(_target)
    _base._association = _association.inverse_association(_target) unless _base._association
    _base.parentize(_target)
    if _base.embedded_many?
      _target.send(_association.inverse(_target)).push(_base)
    else
      remove_associated(_target)
      try_method(_target, _association.inverse_setter(_target), _base)
    end
  end
end

#check_polymorphic_inverses!(doc) (private)

This method is for internal use only.

Check for problems with multiple inverse definitions.

Examples:

Check for inverses errors.

binding.check_inverses!(doc)

Parameters:

  • doc (Document)

    The document to check.

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/embedded_in/binding.rb', line 62

def check_polymorphic_inverses!(doc)
  if inverses = _association.inverses(doc)
    if inverses.length > 1
      raise Errors::InvalidSetPolymorphicRelation.new(
          _association.name, _base.class.name, _target.class.name
      )
    end
  end
end

#unbind_one

Unbinds the base object and the inverse, caused by setting the reference to nil.

Examples:

Unbind the document.

name.person.unbind(:continue => true)
name.person = nil
[ GitHub ]

  
# File 'lib/mongoid/association/embedded/embedded_in/binding.rb', line 42

def unbind_one
  binding do
    if _base.embedded_many?
      _target.send(_association.inverse(_target)).delete(_base)
    else
      try_method(_target, _association.inverse_setter(_target), nil)
    end
  end
end