Class: Humanoid::Associations::BelongsTo

Inherits:
Object
  • Object
show all
Includes:
Proxy
Defined in:
lib/humanoid/associations/belongs_to.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Proxy

included

Constructor Details

#initialize(target, options) ⇒ BelongsTo

Creates the new association by setting the internal target as the passed in Document. This should be the parent.

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

Options:

target: The parent Document options: The association options



18
19
20
21
# File 'lib/humanoid/associations/belongs_to.rb', line 18

def initialize(target, options)
  @target, @options = target, options
  extends(options)
end

Class Method Details

.instantiate(document, options) ⇒ Object

Creates the new association by setting the internal document as the passed in Document. This should be the parent.

Options:

document: The parent Document options: The association options



39
40
41
42
# File 'lib/humanoid/associations/belongs_to.rb', line 39

def instantiate(document, options)
  target = document._parent
  target.nil? ? nil : new(target, options)
end

.macroObject

Returns the macro used to create the association.



45
46
47
# File 'lib/humanoid/associations/belongs_to.rb', line 45

def macro
  :belongs_to
end

.update(target, child, options) ⇒ Object

Perform an update of the relationship of the parent and child. This is initialized by setting a parent object as the association on the Document. Will properly set a has_one or a has_many.

Returns:

A new BelongsTo association proxy.



56
57
58
59
60
# File 'lib/humanoid/associations/belongs_to.rb', line 56

def update(target, child, options)
  child.parentize(target, options.inverse_of)
  child.notify
  instantiate(child, options)
end

Instance Method Details

#find(id) ⇒ Object

Returns the parent document. The id param is present for compatibility with rails, however this could be overwritten in the future.



26
27
28
# File 'lib/humanoid/associations/belongs_to.rb', line 26

def find(id)
  @target
end