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

#declaration_field_name, #declaration_field_type, #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



48
49
50
51
52
53
54
# File 'lib/dynamoid/associations/single_association.rb', line 48

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



41
42
43
# File 'lib/dynamoid/associations/single_association.rb', line 41

def ==(other)
  target == other
end

#associate(hash_key) ⇒ Object



66
67
68
69
# File 'lib/dynamoid/associations/single_association.rb', line 66

def associate(hash_key)
  target.send(target_association).disassociate(source.hash_key) if target && target_association
  source.update_attribute(source_attribute, Set[hash_key])
end

#create(attributes = {}) ⇒ Object



32
33
34
# File 'lib/dynamoid/associations/single_association.rb', line 32

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

#create!(attributes = {}) ⇒ Object



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

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

#deleteObject



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

def delete
  target.send(target_association).disassociate(source.hash_key) if target && target_association
  disassociate
  target
end

#disassociate(hash_key = nil) ⇒ Object



71
72
73
# File 'lib/dynamoid/associations/single_association.rb', line 71

def disassociate(hash_key=nil)
  source.update_attribute(source_attribute, nil)
end

#empty?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/dynamoid/associations/single_association.rb', line 60

def empty?
  # This is needed to that ActiveSupport's #blank? and #present?
  # methods work as expected for SingleAssociations.
  target.nil?
end

#nil?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/dynamoid/associations/single_association.rb', line 56

def nil?
  target.nil?
end

#setter(object) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dynamoid/associations/single_association.rb', line 10

def setter(object)
  if object.nil?
    delete
    return
  end

  associate(object.hash_key)
  self.target = object
  object.send(target_association).associate(source.hash_key) if target_association
  object
end