Class: Mongoid::Relations::Embedded::In

Inherits:
One show all
Defined in:
lib/mongoid/relations/embedded/in.rb

Overview

This class defines the behaviour necessary to handle relations that are embedded within another relation, either as a single document or multiple documents.

Instance Attribute Summary

Attributes inherited from Proxy

#base, #loaded, #metadata, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from One

#load!, #substitute

Methods inherited from Proxy

#init

Constructor Details

#initialize(base, target, metadata) ⇒ In

Instantiate a new embedded_in relation.

Examples:

Create the new relation.

Embedded::In.new(name, person, )

Parameters:

  • base (Document)

    The document the relation hangs off of.

  • target (Document)

    The target (parent) of the relation.

  • metadata (Metadata)

    The relation metadata.



43
44
45
46
47
48
# File 'lib/mongoid/relations/embedded/in.rb', line 43

def initialize(base, target, )
  init(base, target, ) do
    characterize_one(target)
    base.parentize(target)
  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(meta, object) ⇒ Builder

Return the builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the builder.

Embedded::In.builder(meta, object, person)

Parameters:

  • meta (Metadata)

    The metadata of the relation.

  • object (Document, Hash)

    A document or attributes to build with.

Returns:

  • (Builder)

    A newly instantiated builder object.

Since:

  • 2.0.0.rc.1



101
102
103
# File 'lib/mongoid/relations/embedded/in.rb', line 101

def builder(meta, object)
  Builders::Embedded::In.new(meta, object)
end

.embedded?true

Returns true if the relation is an embedded one. In this case always true.

Examples:

Is this relation embedded?

Embedded::In.embedded?

Returns:

  • (true)

    true.

Since:

  • 2.0.0.rc.1



114
115
116
# File 'lib/mongoid/relations/embedded/in.rb', line 114

def embedded?
  true
end

.macroSymbol

Returns the macro for this relation. Used mostly as a helper in reflection.

Examples:

Get the macro.

Mongoid::Relations::Embedded::In.macro

Returns:

Since:

  • 2.0.0.rc.1



127
128
129
# File 'lib/mongoid/relations/embedded/in.rb', line 127

def macro
  :embedded_in
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.

Examples:

Get the builder.

NestedAttributes::One.builder(attributes, options)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • attributes (Hash)

    The attributes to build with.

  • options (Hash)

    The options for the builder.

Options Hash (options):

  • :allow_destroy (true, false)

    Can documents be deleted?

  • :limit (Integer)

    Max number of documents to create at once.

  • :reject_if (Proc, Symbol)

    If documents match this option then they are ignored.

  • :update_only (true, false)

    Only existing documents can be modified.

Returns:

  • (Builder)

    A newly instantiated nested builder object.

Since:

  • 2.0.0.rc.1



153
154
155
# File 'lib/mongoid/relations/embedded/in.rb', line 153

def nested_builder(, attributes, options)
  Builders::NestedAttributes::One.new(, attributes, options)
end

.stores_foreign_key?false

Tells the caller if this relation is one that stores the foreign key on its own objects.

Examples:

Does this relation store a foreign key?

Embedded::In.stores_foreign_key?

Returns:

  • (false)

    false.

Since:

  • 2.0.0.rc.1



166
167
168
# File 'lib/mongoid/relations/embedded/in.rb', line 166

def stores_foreign_key?
  false
end

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 and dont hit the database twice when setting the relations up.

This is called after first creating the relation, or if a new object is set on the relation.

Examples:

Bind the relation.

name.person.bind(:continue => true)

Parameters:

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

    The options to bind with.

Options Hash (options):

  • :binding (true, false)

    Are we in build mode?

  • :continue (true, false)

    Continue binding the inverse?

Since:

  • 2.0.0.rc.1



28
29
30
31
# File 'lib/mongoid/relations/embedded/in.rb', line 28

def bind(options = {})
  binding.bind(options)
  base.save if target.persisted? && !options[:binding]
end

#unbind(old_target, options = {}) ⇒ Object

Unbinds the base object to the inverse of the relation. This occurs when setting a side of the relation to nil.

Will delete the object if necessary.

Examples:

Unbind the relation.

name.person.unbind(:continue => false)

Parameters:

  • old_target (Proxy)

    The previous target of the relation.

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

    The options to bind with.

Options Hash (options):

  • :binding (true, false)

    Are we in build mode?

  • :continue (true, false)

    Continue binding the inverse?

Since:

  • 2.0.0.rc.1



66
67
68
69
# File 'lib/mongoid/relations/embedded/in.rb', line 66

def unbind(old_target, options = {})
  binding(old_target).unbind(options)
  base.delete if old_target.persisted? && !base.destroyed?
end