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, **kwargs, &block) ⇒ Object

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

Since:

  • 0.2.0



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

def method_missing(method, *args, &block)
  if target.respond_to?(method)
    target.send(method, *args, &block)
  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



111
112
113
114
# File 'lib/dynamoid/associations/single_association.rb', line 111

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



117
118
119
# File 'lib/dynamoid/associations/single_association.rb', line 117

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

#empty?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/dynamoid/associations/single_association.rb', line 104

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)


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

def nil?
  target.nil?
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method_name, include_private = false)
  target.respond_to?(method_name, include_private) || super
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