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, #disassociate_source, #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


71
72
73
74
75
76
77
# File 'lib/dynamoid/associations/single_association.rb', line 71

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


63
64
65
# File 'lib/dynamoid/associations/single_association.rb', line 63

def ==(other)
  target == other
end

#associate(hash_key) ⇒ Object


92
93
94
95
# File 'lib/dynamoid/associations/single_association.rb', line 92

def associate(hash_key)
  disassociate_source
  source.update_attribute(source_attribute, Set[hash_key])
end

#create(attributes = {}) ⇒ Dynamoid::Document

Create a new instance of the target class, persist it and associate.

post..create(hight: 50, width: 90)

Parameters:

  • attributes (Hash) (defaults to: {})

    attributes of a model to create

Returns:


54
55
56
# File 'lib/dynamoid/associations/single_association.rb', line 54

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

#create!(attributes = {}) ⇒ Dynamoid::Document

Create a new instance of the target class, persist it and associate.

post..create!(hight: 50, width: 90)

If the creation fails an exception will be raised.

Parameters:

  • attributes (Hash) (defaults to: {})

    attributes of a model to create

Returns:


44
45
46
# File 'lib/dynamoid/associations/single_association.rb', line 44

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

#deleteObject

Delete a model from the association.

post..delete # => nil

Saves both models immediately - a source model and a target one so any unsaved changes will be saved. Doesn't delete an associated model from DynamoDB.


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

def delete
  disassociate_source
  disassociate
  target
end

#disassociate(_hash_key = nil) ⇒ Object


98
99
100
# File 'lib/dynamoid/associations/single_association.rb', line 98

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

#empty?Boolean

Returns:

  • (Boolean)

85
86
87
88
89
# File 'lib/dynamoid/associations/single_association.rb', line 85

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)

80
81
82
# File 'lib/dynamoid/associations/single_association.rb', line 80

def nil?
  target.nil?
end

#setter(object) ⇒ Object


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

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