Class: Dynamoid::TransactionWrite::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid/transaction_write/action.rb

Direct Known Subclasses

Create, Delete, Destroy, UpdateUpsert

Constant Summary collapse

VALID_OPTIONS =
%i[skip_callbacks skip_validation raise_validation_error skip_existence_check].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_or_model_class, attributes = {}, options = {}) {|_self| ... } ⇒ Action

Returns a new instance of Action.

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dynamoid/transaction_write/action.rb', line 9

def initialize(model_or_model_class, attributes = {}, options = {})
  if model_or_model_class.is_a?(Dynamoid::Document)
    self.model = model_or_model_class
  else
    @model_class = model_or_model_class
  end
  self.attributes = attributes
  self.options = options || {}
  self.additions = {}
  self.deletions = {}
  self.removals = []
  invalid_keys = self.options.keys - VALID_OPTIONS
  raise ArgumentError, "Invalid options found: '#{invalid_keys}'" if invalid_keys.present?

  yield(self) if block_given?
end

Instance Attribute Details

#additionsObject

Returns the value of attribute additions.



7
8
9
# File 'lib/dynamoid/transaction_write/action.rb', line 7

def additions
  @additions
end

#attributesObject

Returns the value of attribute attributes.



7
8
9
# File 'lib/dynamoid/transaction_write/action.rb', line 7

def attributes
  @attributes
end

#deletionsObject

Returns the value of attribute deletions.



7
8
9
# File 'lib/dynamoid/transaction_write/action.rb', line 7

def deletions
  @deletions
end

#modelObject

Returns the value of attribute model.



7
8
9
# File 'lib/dynamoid/transaction_write/action.rb', line 7

def model
  @model
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/dynamoid/transaction_write/action.rb', line 7

def options
  @options
end

#removalsObject

Returns the value of attribute removals.



7
8
9
# File 'lib/dynamoid/transaction_write/action.rb', line 7

def removals
  @removals
end

Instance Method Details

#add(values) ⇒ Object

increments a number or adds to a set, starts at 0 or [] if it doesn’t yet exist



51
52
53
# File 'lib/dynamoid/transaction_write/action.rb', line 51

def add(values)
  additions.merge!(values)
end

#add_timestamps(attributes, skip_created_at: false) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/dynamoid/transaction_write/action.rb', line 102

def add_timestamps(attributes, skip_created_at: false)
  return attributes if options[:skip_timestamps] || !model_class&.timestamps_enabled?

  result = attributes.clone
  timestamp = DateTime.now.in_time_zone(Time.zone)
  result[:created_at] ||= timestamp unless skip_created_at
  result[:updated_at] ||= timestamp
  result
end

#changes_appliedObject



98
99
100
# File 'lib/dynamoid/transaction_write/action.rb', line 98

def changes_applied
  !!model&.changes_applied
end

#delete(field_or_values) ⇒ Object

deletes a value or values from a set type or simply sets a field to nil



56
57
58
59
60
61
62
63
# File 'lib/dynamoid/transaction_write/action.rb', line 56

def delete(field_or_values)
  if field_or_values.is_a?(Hash)
    deletions.merge!(field_or_values)
  else
    # adds to array of fields for use in REMOVE update expression
    removals << field_or_values
  end
end

#find_from_attributes(model_or_model_class, attributes) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dynamoid/transaction_write/action.rb', line 65

def find_from_attributes(model_or_model_class, attributes)
  model_class = model_or_model_class.is_a?(Dynamoid::Document) ? model_or_model_class.class : model_or_model_class
  if attributes.is_a?(Hash)
    raise Dynamoid::Errors::MissingHashKey unless attributes[model_class.hash_key].present?

    model_class.find(attributes[model_class.hash_key],
                     range_key: model_class.range_key? ? attributes[model_class.range_key] : nil,
                     consistent_read: true)
  else
    model_class.find(attributes, consistent_read: true)
  end
end

#hash_keyObject

returns hash_key from the model if it exists or first element from id



31
32
33
34
35
# File 'lib/dynamoid/transaction_write/action.rb', line 31

def hash_key
  return model.hash_key if model

  attributes[model_class.hash_key]
end

#model_classObject



26
27
28
# File 'lib/dynamoid/transaction_write/action.rb', line 26

def model_class
  model&.class || @model_class
end

#raise_validation_error?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/dynamoid/transaction_write/action.rb', line 90

def raise_validation_error?
  !!options[:raise_validation_error]
end

#range_keyObject

returns range_key from the model if it exists or second element from id, or nil



38
39
40
41
42
43
# File 'lib/dynamoid/transaction_write/action.rb', line 38

def range_key
  return nil unless model_class.range_key?
  return model.attributes[model_class.range_key] if model

  attributes[model_class.range_key]
end

#run_callbacksObject



94
95
96
# File 'lib/dynamoid/transaction_write/action.rb', line 94

def run_callbacks
  yield if block_given?
end

#sanitize_item(attributes) ⇒ Object

copied from the protected method in AwsSdkV3



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dynamoid/transaction_write/action.rb', line 127

def sanitize_item(attributes)
  config_value = Dynamoid.config.store_attribute_with_nil_value
  store_attribute_with_nil_value = config_value.nil? ? false : !!config_value

  attributes.reject do |_, v|
    ((v.is_a?(Set) || v.is_a?(String)) && v.empty?) ||
      (!store_attribute_with_nil_value && v.nil?)
  end.transform_values do |v|
    v.is_a?(Hash) ? v.stringify_keys : v
  end
end

#set(values) ⇒ Object

sets a value in the attributes



46
47
48
# File 'lib/dynamoid/transaction_write/action.rb', line 46

def set(values)
  attributes.merge!(values)
end

#skip_callbacks?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/dynamoid/transaction_write/action.rb', line 78

def skip_callbacks?
  !!options[:skip_callbacks]
end

#skip_validation?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/dynamoid/transaction_write/action.rb', line 82

def skip_validation?
  !!options[:skip_validation]
end

#to_hObject



139
140
141
# File 'lib/dynamoid/transaction_write/action.rb', line 139

def to_h
  raise 'override me'
end

#touch_model_timestamps(skip_created_at: false) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/dynamoid/transaction_write/action.rb', line 112

def touch_model_timestamps(skip_created_at: false)
  return if !model || options[:skip_timestamps] || !model_class.timestamps_enabled?

  timestamp = DateTime.now.in_time_zone(Time.zone)
  model.updated_at = timestamp
  model.created_at ||= timestamp unless skip_created_at
end

#valid?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/dynamoid/transaction_write/action.rb', line 86

def valid?
  model&.valid?
end

#write_attributes_to_modelObject



120
121
122
123
124
# File 'lib/dynamoid/transaction_write/action.rb', line 120

def write_attributes_to_model
  return unless model && attributes.present?

  attributes.each { |attribute, value| model.write_attribute(attribute, value) }
end