Module: Dynamoid::Associations::SingleAssociation

Includes:
Association
Included in:
BelongsTo, HasOne
Defined in:
lib/dynamoid/associations/single_association.rb

Instance Attribute Summary

Attributes included from Association

#loaded, #name, #options, #source

Instance Method Summary collapse

Methods included from Association

#initialize, #loaded?, #reset, #target

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Delegate methods we don’t find directly to the target.

Since:

  • 0.2.0



44
45
46
47
48
49
50
# File 'lib/dynamoid/associations/single_association.rb', line 44

def method_missing(method, *args)
  if target.respond_to?(method)
    target.send(method, *args)
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Is this object equal to the association’s target?

Returns:

  • (Boolean)

    true/false

Since:

  • 0.2.0



37
38
39
# File 'lib/dynamoid/associations/single_association.rb', line 37

def ==(other)
  target == other
end

#create(attributes = {}) ⇒ Object



27
28
29
# File 'lib/dynamoid/associations/single_association.rb', line 27

def create(attributes = {})
  setter(target_class.create!(attributes))
end

#create!(attributes = {}) ⇒ Object



23
24
25
# File 'lib/dynamoid/associations/single_association.rb', line 23

def create!(attributes = {})
  setter(target_class.create!(attributes))
end

#deleteObject



17
18
19
20
21
# File 'lib/dynamoid/associations/single_association.rb', line 17

def delete
  source.update_attribute(source_attribute, nil)
  self.send(:disassociate_target, target) if target && target_association
  target
end

#nil?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/dynamoid/associations/single_association.rb', line 52

def nil?
  target.nil?
end

#setter(object) ⇒ Object



10
11
12
13
14
15
# File 'lib/dynamoid/associations/single_association.rb', line 10

def setter(object)
  delete
  source.update_attribute(source_attribute, Set[object.id])
  self.send(:associate_target, object) if target_association
  object
end