Module: Dynomite::Associations::SingleAssociation

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

Instance Attribute Summary

Attributes included from Association

#loaded, #name, #options, #source

Instance Method Summary collapse

Methods included from Association

#coerce_to_id, #coerce_to_item, #declaration_field_type, #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.



108
109
110
111
112
113
114
# File 'lib/dynomite/associations/single_association.rb', line 108

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 item equal to the association’s target?

Returns:

  • (Boolean)

    true/false



102
103
104
# File 'lib/dynomite/associations/single_association.rb', line 102

def ==(other)
  target == other
end

#associate(item) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dynomite/associations/single_association.rb', line 23

def associate(item)
  item = coerce_to_item(item)
  associate_one_way(item)

  # inverse relationship
  should_reload = false
  Array(target).each do |target_entry|
    if target_entry && target_association
      target_entry.send(target_association).associate_one_way(source)
      should_reload = true
    end
  end
  target.reload if should_reload

  self.target = item
end

#associate_one_way(item) ⇒ Object



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

def associate_one_way(item)
  # normal relationship
  source.update_attribute_presence(source_attribute, coerce_to_id(item))
end

#create(attributes = {}) ⇒ Dynomite::Item

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:



95
96
97
# File 'lib/dynomite/associations/single_association.rb', line 95

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

#create!(attributes = {}) ⇒ Dynomite::Item

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:



85
86
87
# File 'lib/dynomite/associations/single_association.rb', line 85

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

#declaration_field_nameObject

target field name. IE: posts.user_id



7
8
9
# File 'lib/dynomite/associations/single_association.rb', line 7

def declaration_field_name
  options[:foreign_key] || "#{name}_id"
end

#disassociate(_ = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dynomite/associations/single_association.rb', line 45

def disassociate(_ = nil)
  # inverse relationship: user.posts removal. run first before target is nil
  should_reload = false
  Array(target).each do |target_entry|
    if target_entry && target_association
      target_entry.send(target_association).disassociate_one_way(source)
      should_reload = true
    end
  end
  target.reload if should_reload

  # normal relationship: post.user removal
  disassociate_one_way

  self.target = nil
end

#disassociate_one_way(_ = nil) ⇒ Object

Delete a model from the association.

post..disassociate # => 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.

_ = nil so can keep the same interface for removing has_many_and_belongs_to associations


72
73
74
75
# File 'lib/dynomite/associations/single_association.rb', line 72

def disassociate_one_way(_ = nil)
  # normal relationship: post.user removal
  source.update_attribute_presence(source_attribute, nil)
end

#empty?Boolean

Returns:

  • (Boolean)


134
135
136
137
138
# File 'lib/dynomite/associations/single_association.rb', line 134

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)


130
131
132
# File 'lib/dynomite/associations/single_association.rb', line 130

def nil?
  target.nil?
end

#reader_targetObject



11
12
13
# File 'lib/dynomite/associations/single_association.rb', line 11

def reader_target
  target
end

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

Returns:

  • (Boolean)


126
127
128
# File 'lib/dynomite/associations/single_association.rb', line 126

def respond_to_missing?(method_name, include_private = false)
  target.respond_to?(method_name, include_private) || super
end

#setter(item) ⇒ Object



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

def setter(item)
  if item.nil?
    disassociate
  else
    associate(item)
  end
end