Methods

Class/Module Index [+]

Quicksearch

Mongoid::Relations::Embedded::One

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

Public Class Methods

new(base, target, metadata) click to toggle source

Instantiate a new embeds_one relation.

@example Create the new proxy.

One.new(person, name, metadata)

@param [ Document ] base The document this relation hangs off of. @param [ Document ] target The child document in the relation. @param [ Metadata ] metadata The relation's metadata

# File lib/mongoid/relations/embedded/one.rb, line 17
def initialize(base, target, metadata)
  init(base, target, metadata) do
    characterize_one(target)
    bind_one
    characterize_one(target)
    target.save if persistable?
  end
end

Public Instance Methods

substitute(replacement) click to toggle source

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

@example Substitute the new document.

person.name.substitute(new_name)

@param [ Document ] other A document to replace the target.

@return [ Document, nil ] The relation or nil.

@since 2.0.0.rc.1

# File lib/mongoid/relations/embedded/one.rb, line 37
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.