Class: Mongoid::Relations::Embedded::One

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

Overview

This class defines the behaviour needed for embedded one to one relations.

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) ⇒ One

Instantiate a new embeds_one relation.

Examples:

Create the new proxy.

One.new(person, name, )

Parameters:

  • base (Document)

    The document this relation hangs off of.

  • target (Document)

    The child document in the relation.

  • metadata (Metadata)

    The relation’s metadata



40
41
42
43
44
45
# File 'lib/mongoid/relations/embedded/one.rb', line 40

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



98
99
100
# File 'lib/mongoid/relations/embedded/one.rb', line 98

def builder(meta, object)
  Builders::Embedded::One.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::One.embedded?

Returns:

  • (true)

    true.

Since:

  • 2.0.0.rc.1



111
112
113
# File 'lib/mongoid/relations/embedded/one.rb', line 111

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::One.macro

Returns:

Since:

  • 2.0.0.rc.1



124
125
126
# File 'lib/mongoid/relations/embedded/one.rb', line 124

def macro
  :embeds_one
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



150
151
152
# File 'lib/mongoid/relations/embedded/one.rb', line 150

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::One.stores_foreign_key?

Returns:

  • (false)

    false.

Since:

  • 2.0.0.rc.1



163
164
165
# File 'lib/mongoid/relations/embedded/one.rb', line 163

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.

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

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



27
28
29
30
# File 'lib/mongoid/relations/embedded/one.rb', line 27

def bind(options = {})
  binding.bind(options)
  target.save if base.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.

person.name.unbind(name, :continue => true)

Parameters:

  • old_target (Document)

    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



63
64
65
66
# File 'lib/mongoid/relations/embedded/one.rb', line 63

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