Class: Mongoid::Associations::HasOne

Inherits:
Object
  • Object
show all
Includes:
Proxy
Defined in:
lib/mongoid/associations/has_one.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Proxy

included

Constructor Details

#initialize(document, attributes, options) ⇒ HasOne

Creates the new association by finding the attributes in the parent document with its name, and instantiating a new document for it.

All method calls on this object will then be delegated to the internal document itself.

Options:

document: The parent Document attributes: The attributes of the decorated object. options: The association options.



34
35
36
37
38
39
# File 'lib/mongoid/associations/has_one.rb', line 34

def initialize(document, attributes, options)
  @parent, @options, @association_name = document, options, options.name
  unless attributes.nil?
    @document = attributes.assimilate(@parent, @options)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Delegate all missing methods over to the Document.



42
43
44
45
# File 'lib/mongoid/associations/has_one.rb', line 42

def method_missing(name, *args, &block)
  @document = {}.assimilate(@parent, @options) if @document.nil?
  @document.send(name, *args, &block)
end

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



7
8
9
# File 'lib/mongoid/associations/has_one.rb', line 7

def association_name
  @association_name
end

#documentObject (readonly)

Returns the value of attribute document.



7
8
9
# File 'lib/mongoid/associations/has_one.rb', line 7

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/mongoid/associations/has_one.rb', line 7

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/mongoid/associations/has_one.rb', line 7

def parent
  @parent
end

Class Method Details

.instantiate(document, options) ⇒ Object

Preferred method of instantiating a new HasOne, since nil values will be handled properly.

Options:

document: The parent Document options: The association options.



66
67
68
69
# File 'lib/mongoid/associations/has_one.rb', line 66

def instantiate(document, options)
  attributes = document.attributes[options.name]
  new(document, attributes, options)
end

.macroObject

Returns the macro used to create the association.



72
73
74
# File 'lib/mongoid/associations/has_one.rb', line 72

def macro
  :has_one
end

.update(child, parent, options) ⇒ Object

Perform an update of the relationship of the parent and child. This will assimilate the child Document into the parent’s object graph.

Options:

child: The child Document or Hash. parent: The parent Document to update. options: The association Options

Example:

HasOne.update({:first_name => "Hank"}, person, options)



88
89
90
# File 'lib/mongoid/associations/has_one.rb', line 88

def update(child, parent, options)
  child.assimilate(parent, options)
end

Instance Method Details

#build(attributes) ⇒ Object

Build a new object for the association.



10
11
12
13
# File 'lib/mongoid/associations/has_one.rb', line 10

def build(attributes)
  @document = attributes.assimilate(@parent, @options)
  self
end

#create(attributes) ⇒ Object

Create a new object for the association and save it.



16
17
18
19
20
# File 'lib/mongoid/associations/has_one.rb', line 16

def create(attributes)
  build(attributes)
  @document.save
  self
end

#nested_build(attributes) ⇒ Object

Used for setting the association via a nested attributes setter on the parent Document.



49
50
51
# File 'lib/mongoid/associations/has_one.rb', line 49

def nested_build(attributes)
  build(attributes)
end

#valid?Boolean

Need to override here for when the underlying document is nil.

Returns:



54
55
56
# File 'lib/mongoid/associations/has_one.rb', line 54

def valid?
  @document ? @document.valid? : false
end