Class: Dynamoid::TransactionWrite::Destroy

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

Constant Summary

Constants inherited from Action

Action::VALID_OPTIONS

Instance Attribute Summary

Attributes inherited from Action

#additions, #attributes, #deletions, #model, #options, #removals

Instance Method Summary collapse

Methods inherited from Action

#add, #add_timestamps, #changes_applied, #delete, #find_from_attributes, #hash_key, #model_class, #raise_validation_error?, #range_key, #sanitize_item, #set, #skip_callbacks?, #touch_model_timestamps, #valid?, #write_attributes_to_model

Constructor Details

#initialize(model_or_model_class, key_or_attributes = {}, options = {}) ⇒ Destroy

model is found if not otherwise specified so callbacks can be run



9
10
11
12
13
14
15
16
17
18
# File 'lib/dynamoid/transaction_write/destroy.rb', line 9

def initialize(model_or_model_class, key_or_attributes = {}, options = {})
  model = if model_or_model_class.is_a?(Dynamoid::Document)
            model_or_model_class
          else
            find_from_attributes(model_or_model_class, key_or_attributes)
          end
  super(model, {}, options)
  raise Dynamoid::Errors::MissingHashKey unless hash_key.present?
  raise Dynamoid::Errors::MissingRangeKey unless !model_class.range_key? || range_key.present?
end

Instance Method Details

#run_callbacksObject



20
21
22
23
24
# File 'lib/dynamoid/transaction_write/destroy.rb', line 20

def run_callbacks
  model.run_callbacks(:destroy) do
    yield if block_given?
  end
end

#skip_validation?Boolean

destroy defaults to not using validation

Returns:

  • (Boolean)


27
28
29
# File 'lib/dynamoid/transaction_write/destroy.rb', line 27

def skip_validation?
  options[:skip_validation] != false
end

#to_hObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dynamoid/transaction_write/destroy.rb', line 31

def to_h
  model.destroyed = true
  key = { model_class.hash_key => hash_key }
  key[model_class.range_key] = range_key if model_class.range_key?
  # force fast fail i.e. won't retry if record is missing
  condition_expression = "attribute_exists(#{model_class.hash_key})"
  condition_expression += " and attribute_exists(#{model_class.range_key})" if model_class.range_key? # needed?
  result = {
    delete: {
      key: key,
      table_name: model_class.table_name
    }
  }
  result[:delete][:condition_expression] = condition_expression unless options[:skip_existence_check]
  result
end