Class: Mongoid::Relations::Bindings::Referenced::Many

Inherits:
Mongoid::Relations::Binding show all
Defined in:
lib/mongoid/relations/bindings/referenced/many.rb

Overview

Binding class for references_many relations.

Constant Summary

Constants inherited from Mongoid::Relations::Binding

Mongoid::Relations::Binding::OPTIONS

Instance Attribute Summary

Attributes inherited from Mongoid::Relations::Binding

#base, #metadata, #target

Instance Method Summary collapse

Methods inherited from Mongoid::Relations::Binding

#initialize

Constructor Details

This class inherits a constructor from Mongoid::Relations::Binding

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 on both sides.

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

Examples:

Bind all the documents.

person.posts.bind
person.posts = [ Post.new ]

Parameters:

  • options (Hash) (defaults to: {})

    The binding options.

Options Hash (options):

  • :continue (true, false)

    Continue binding the inverse.

  • :binding (true, false)

    Are we in build mode?

Since:

  • 2.0.0.rc.1



26
27
28
# File 'lib/mongoid/relations/bindings/referenced/many.rb', line 26

def bind(options = {})
  target.each { |doc| bind_one(doc, options) }
end

#bind_one(doc, options = {}) ⇒ Object

Binds a single document with the inverse relation. Used specifically when appending to the proxy.

Examples:

Bind one document.

person.posts.bind_one(post)

Parameters:

  • doc (Document)

    The single document to bind.

  • options (Hash) (defaults to: {})

    The binding options.

Options Hash (options):

  • :continue (true, false)

    Continue binding the inverse.

  • :binding (true, false)

    Are we in build mode?

Since:

  • 2.0.0.rc.1



43
44
45
46
47
48
49
50
51
52
# File 'lib/mongoid/relations/bindings/referenced/many.rb', line 43

def bind_one(doc, options = {})
  if options[:continue]
    doc.do_or_do_not(.foreign_key_setter, base.id)
    doc.do_or_do_not(
      .inverse_setter,
      base,
      OPTIONS
    )
  end
end

#unbind(options = {}) ⇒ Object

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

Examples:

Unbind the documents.

person.posts.unbind
person.posts = nil

Parameters:

  • options (Hash) (defaults to: {})

    The binding options.

Options Hash (options):

  • :continue (true, false)

    Continue binding the inverse.

  • :binding (true, false)

    Are we in build mode?

Since:

  • 2.0.0.rc.1



67
68
69
# File 'lib/mongoid/relations/bindings/referenced/many.rb', line 67

def unbind(options = {})
  target.each { |doc| unbind_one(doc, options) }
end

#unbind_one(doc, options = {}) ⇒ Object

Unbind a single document.

Examples:

Unbind the document.

person.posts.unbind_one(document)

Parameters:

  • options (Hash) (defaults to: {})

    The binding options.

Options Hash (options):

  • :continue (true, false)

    Continue binding the inverse.

  • :binding (true, false)

    Are we in build mode?

Since:

  • 2.0.0.rc.1



82
83
84
85
86
87
88
89
90
91
# File 'lib/mongoid/relations/bindings/referenced/many.rb', line 82

def unbind_one(doc, options = {})
  if options[:continue]
    doc.do_or_do_not(.foreign_key_setter, nil)
    doc.do_or_do_not(
      .inverse_setter,
      nil,
      OPTIONS
    )
  end
end