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

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

Overview

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

Constant Summary collapse

VALID_OPTIONS =

The allowed options when defining this relation.

Returns:

  • (Array<Symbol>)

    The allowed options when defining this relation.

Since:

  • 6.0.0

[
  :autobuild,
  :as,
  :cascade_callbacks,
  :cyclic,
  :store_as
].freeze

Instance Attribute Summary

Attributes inherited from Proxy

#__metadata, #base, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from One

#__evolve_object_id__, #clear, #in_memory, #respond_to?

Methods inherited from Proxy

apply_ordering, #extend_proxies, #init, #klass, #reset_unloaded, #substitutable

Methods included from Marshalable

#marshal_dump, #marshal_load

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



31
32
33
34
35
36
37
38
39
# File 'lib/mongoid/relations/embedded/one.rb', line 31

def initialize(base, target, )
  init(base, target, ) do
    characterize_one(target)
    bind_one
    characterize_one(target)
    base._reset_memoized_children!
    target.save if persistable?
  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::One.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



113
114
115
# File 'lib/mongoid/relations/embedded/one.rb', line 113

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

Returns:

  • (true)

    true.

Since:

  • 2.0.0.rc.1



126
127
128
# File 'lib/mongoid/relations/embedded/one.rb', line 126

def embedded?
  true
end

.foreign_key_suffixnil

Returns the suffix of the foreign key field, either “_id” or “_ids”.

Examples:

Get the suffix for the foreign key.

Referenced::Many.foreign_key_suffix

Returns:

  • (nil)

    nil.

Since:

  • 3.0.0



138
139
140
# File 'lib/mongoid/relations/embedded/one.rb', line 138

def foreign_key_suffix
  nil
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



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

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



177
178
179
# File 'lib/mongoid/relations/embedded/one.rb', line 177

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

.path(document) ⇒ Mongoid::Atomic::Paths::Embedded::One

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:

Since:

  • 2.1.0



192
193
194
# File 'lib/mongoid/relations/embedded/one.rb', line 192

def path(document)
  Mongoid::Atomic::Paths::Embedded::One.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::One.stores_foreign_key?

Returns:

  • (false)

    false.

Since:

  • 2.0.0.rc.1



205
206
207
# File 'lib/mongoid/relations/embedded/one.rb', line 205

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:

  • (Array<Symbol>)

    The valid options.

Since:

  • 2.1.0



217
218
219
# File 'lib/mongoid/relations/embedded/one.rb', line 217

def valid_options
  VALID_OPTIONS
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



230
231
232
# File 'lib/mongoid/relations/embedded/one.rb', line 230

def validation_default
  true
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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mongoid/relations/embedded/one.rb', line 52

def substitute(replacement)
  if replacement != self
    if _assigning?
      base.add_atomic_unset(target) unless replacement
    else
      target.destroy if persistable?
    end
    unbind_one
    return nil unless replacement
    replacement = Factory.build(klass, replacement) if replacement.is_a?(::Hash)
    self.target = replacement
    bind_one
    characterize_one(target)
    target.save if persistable? && !_assigning?
  end
  self
end