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
40
# 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?
    klass = attributes[:_type] ? attributes[:_type].constantize : nil
    @document = attributes.assimilate(@parent, @options, klass)
  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.



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

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.



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

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

.macroObject

Returns the macro used to create the association.



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

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)



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

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

Instance Method Details

#build(attrs = {}, type = nil) ⇒ Object

Build a new object for the association.



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

def build(attrs = {}, type = nil)
  @document = attrs.assimilate(@parent, @options, type)
  self
end

#create(attrs = {}, type = nil) ⇒ 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(attrs = {}, type = nil)
  build(attrs, type)
  @document.save
  self
end

#nested_build(attributes) ⇒ Object

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



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

def nested_build(attributes)
  build(attributes)
end

#valid?Boolean

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

Returns:



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

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