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

#in_memory, #respond_to?

Methods inherited from Proxy

#init, #substitutable

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.



21
22
23
24
25
26
# File 'lib/mongoid/relations/embedded/in.rb', line 21

def initialize(base, target, )
  init(base, target, ) do
    characterize_one(target)
    bind_one
  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(base, 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:

  • base (Document)

    The base document.

  • 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



109
110
111
# File 'lib/mongoid/relations/embedded/in.rb', line 109

def builder(base, meta, object)
  Builders::Embedded::In.new(base, 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



122
123
124
# File 'lib/mongoid/relations/embedded/in.rb', line 122

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



135
136
137
# File 'lib/mongoid/relations/embedded/in.rb', line 135

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



161
162
163
# File 'lib/mongoid/relations/embedded/in.rb', line 161

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

.path(document) ⇒ Root

Get the path calculator for the supplied document.

Examples:

Get the path calculator.

Proxy.path(document)

Parameters:

  • document (Document)

    The document to calculate on.

Returns:

  • (Root)

    The root atomic path calculator.

Since:

  • 2.1.0



175
176
177
# File 'lib/mongoid/relations/embedded/in.rb', line 175

def path(document)
  Mongoid::Atomic::Paths::Root.new(document)
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



188
189
190
# File 'lib/mongoid/relations/embedded/in.rb', line 188

def stores_foreign_key?
  false
end

.valid_optionsArray<Symbol>

Get the valid options allowed with this relation.

Examples:

Get the valid options.

Relation.valid_options

Returns:

Since:

  • 2.1.0



200
201
202
# File 'lib/mongoid/relations/embedded/in.rb', line 200

def valid_options
  [ :cyclic, :polymorphic ]
end

.validation_defaulttrue, false

Get the default validation setting for the relation. Determines if by default a validates associated will occur.

Examples:

Get the validation default.

Proxy.validation_default

Returns:

  • (true, false)

    The validation default.

Since:

  • 2.1.9



213
214
215
# File 'lib/mongoid/relations/embedded/in.rb', line 213

def validation_default
  false
end

Instance Method Details

#substitute(replacement) ⇒ Document?

Substitutes the supplied target documents for the existing document in the relation.

Examples:

Substitute the new document.

person.name.substitute(new_name)

Parameters:

  • other (Document)

    A document to replace the target.

Returns:

  • (Document, nil)

    The relation or nil.

Since:

  • 2.0.0.rc.1



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mongoid/relations/embedded/in.rb', line 39

def substitute(replacement)
  tap do |proxy|
    proxy.unbind_one
    unless replacement
      base.delete if persistable?
      return nil
    end
    base.new_record = true
    proxy.target = replacement
    proxy.bind_one
  end
end