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
41
42
# 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)
  else
    @document = nil
  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.



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

def method_missing(name, *args, &block)
  @document.send(name, *args, &block) unless @document.nil?
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.



78
79
80
81
# File 'lib/mongoid/associations/has_one.rb', line 78

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

.macroObject

Returns the macro used to create the association.



84
85
86
# File 'lib/mongoid/associations/has_one.rb', line 84

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)



100
101
102
103
104
105
106
# File 'lib/mongoid/associations/has_one.rb', line 100

def update(child, parent, options)
  if child.nil?
    return parent.attributes[options.name] = nil
  else
    return child.assimilate(parent, options)
  end
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.



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

def nested_build(attributes)
  build(attributes)
end

#nil?Boolean

This should delegate to the document.

Returns:



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

def nil?
  @document.nil?
end

#to_aObject

This will get deprecated



61
62
63
# File 'lib/mongoid/associations/has_one.rb', line 61

def to_a
  [@document]
end

#valid?Boolean

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

Returns:



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

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