Class: Dynamoid::TransactionWrite::Upsert

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

Instance Method Summary collapse

Methods inherited from Base

#sanitize_item

Constructor Details

#initialize(model_class, hash_key, range_key, attributes) ⇒ Upsert



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

def initialize(model_class, hash_key, range_key, attributes)
  super()

  @model_class = model_class
  @hash_key = hash_key
  @range_key = range_key
  @attributes = attributes
end

Instance Method Details

#aborted?Boolean



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

def aborted?
  false
end

#action_requestObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dynamoid/transaction_write/upsert.rb', line 40

def action_request
  # changed attributes to persist
  changes = @attributes.dup
  changes = add_timestamps(changes, skip_created_at: true)
  changes_dumped = Dynamoid::Dumping.dump_attributes(changes, @model_class.attributes)

  # primary key to look up an item to update
  partition_key_dumped = dump(@model_class.hash_key, @hash_key)
  key = { @model_class.hash_key => partition_key_dumped }

  if @model_class.range_key?
    sort_key_dumped = dump(@model_class.range_key, @range_key)
    key[@model_class.range_key] = sort_key_dumped
  end

  # Build UpdateExpression and keep names and values placeholders mapping
  # in ExpressionAttributeNames and ExpressionAttributeValues.
  set_expression_statements = []
  remove_expression_statements = []
  expression_attribute_names = {}
  expression_attribute_values = {}

  changes_dumped.each_with_index do |(name, value), i|
    name_placeholder = "#_n#{i}"
    value_placeholder = ":_s#{i}"

    if value || Dynamoid.config.store_attribute_with_nil_value
      set_expression_statements << "#{name_placeholder} = #{value_placeholder}"
      expression_attribute_values[value_placeholder] = value
    else
      remove_expression_statements << name_placeholder
    end
    expression_attribute_names[name_placeholder] = name
  end

  update_expression = ''
  update_expression += "SET #{set_expression_statements.join(', ')}" if set_expression_statements.any?
  update_expression += " REMOVE #{remove_expression_statements.join(', ')}" if remove_expression_statements.any?

  {
    update: {
      key: key,
      table_name: @model_class.table_name,
      update_expression: update_expression,
      expression_attribute_names: expression_attribute_names,
      expression_attribute_values: expression_attribute_values
    }
  }
end

#observable_by_user_resultObject



36
37
38
# File 'lib/dynamoid/transaction_write/upsert.rb', line 36

def observable_by_user_result
  nil
end

#on_commitObject



23
# File 'lib/dynamoid/transaction_write/upsert.rb', line 23

def on_commit; end

#on_registrationObject



18
19
20
21
# File 'lib/dynamoid/transaction_write/upsert.rb', line 18

def on_registration
  validate_primary_key!
  Dynamoid::Persistence::UpdateValidations.validate_attributes_exist(@model_class, @attributes)
end

#on_rollbackObject



25
# File 'lib/dynamoid/transaction_write/upsert.rb', line 25

def on_rollback; end

#skipped?Boolean



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

def skipped?
  attributes_to_assign = @attributes.except(@model_class.hash_key, @model_class.range_key)
  attributes_to_assign.empty? && !@model_class.timestamps_enabled?
end