Class: Dynamoid::TransactionWrite::Upsert

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of 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

Returns:

  • (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
# 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
  key = { @model_class.hash_key => @hash_key }
  key[@model_class.range_key] = @range_key if @model_class.range_key?

  # Build UpdateExpression and keep names and values placeholders mapping
  # in ExpressionAttributeNames and ExpressionAttributeValues.
  update_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}"

    update_expression_statements << "#{name_placeholder} = #{value_placeholder}"
    expression_attribute_names[name_placeholder] = name
    expression_attribute_values[value_placeholder] = value
  end

  update_expression = "SET #{update_expression_statements.join(', ')}"

  {
    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

Returns:

  • (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