Class: Dynamoid::Transactions::Mutation::Upsert

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

Instance Method Summary collapse

Methods inherited from Base

validate_attribute_names!

Constructor Details

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

Returns a new instance of Upsert.



11
12
13
14
15
16
17
18
# File 'lib/dynamoid/transactions/mutation/upsert.rb', line 11

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)


29
30
31
# File 'lib/dynamoid/transactions/mutation/upsert.rb', line 29

def aborted?
  false
end

#action_requestsObject



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
# File 'lib/dynamoid/transactions/mutation/upsert.rb', line 42

def action_requests
  # 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)

  builder = Builders::UpdateRequestBuilder.new(@model_class)
  builder.hash_key = cast_and_dump(@model_class.hash_key, @hash_key)
  builder.range_key = cast_and_dump(@model_class.range_key, @range_key) if @model_class.range_key?

  attributes_to_set = {}
  attributes_to_remove = []

  changes_dumped.each do |name, value|
    if value || Dynamoid.config.store_attribute_with_nil_value
      attributes_to_set[name] = value
    else
      attributes_to_remove << name
    end
  end

  builder.set_attributes(attributes_to_set)
  builder.remove_attributes(attributes_to_remove)

  [builder.request]
end

#observable_by_user_resultObject



38
39
40
# File 'lib/dynamoid/transactions/mutation/upsert.rb', line 38

def observable_by_user_result
  nil
end

#on_commitObject



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

def on_commit; end

#on_registrationObject



20
21
22
23
# File 'lib/dynamoid/transactions/mutation/upsert.rb', line 20

def on_registration
  validate_primary_key!
  validate_attribute_names!(@model_class, @attributes.keys)
end

#on_rollbackObject



27
# File 'lib/dynamoid/transactions/mutation/upsert.rb', line 27

def on_rollback; end

#skipped?Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/dynamoid/transactions/mutation/upsert.rb', line 33

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