Class: Dynamoid::TransactionWrite::UpdateFields

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

Defined Under Namespace

Classes: UpdateRequestBuilder

Instance Method Summary collapse

Constructor Details

#initialize(model_class, hash_key, range_key, attributes, &block) ⇒ UpdateFields

Returns a new instance of UpdateFields.



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

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

  @model_class = model_class
  @hash_key = hash_key
  @range_key = range_key
  @attributes = attributes || {}
  @block = block
end

Instance Method Details

#aborted?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 33

def aborted?
  false
end

#action_requestObject



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
89
90
91
92
93
94
95
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 45

def action_request
  builder = UpdateRequestBuilder.new(@model_class)

  # primary key to look up an item to update
  builder.hash_key = dump_attribute(@model_class.hash_key, @hash_key)
  builder.range_key = dump_attribute(@model_class.range_key, @range_key) if @model_class.range_key?

  # 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.set_attributes(changes_dumped)

  # given a block
  if @item_updater
    builder.set_attributes(@item_updater.attributes_to_set)
    builder.remove_attributes(@item_updater.attributes_to_remove)

    @item_updater.attributes_to_add.each do |name, value|
      # The ADD section in UpdateExpressions requires values to be a
      # set to update a set attribute.
      # Allow specifying values as any Enumerable collection (e.g. Array).
      # Allow a single value not wrapped into a Set
      if @model_class.attributes[name][:type] == :set
        value = value.is_a?(Enumerable) ? Set.new(value) : Set[value]
      end

      builder.add_value(name, value)
    end

    @item_updater.attributes_to_delete.each do |name, value|
      # The DELETE section in UpdateExpressions requires values to be a
      # set to update a set attribute.
      # Allow specifying values as any Enumerable collection (e.g. Array).
      # Allow a single value not wrapped into a Set
      value = value.is_a?(Enumerable) ? Set.new(value) : Set[value]

      builder.delete_value(name, value)
    end
  end

  # require primary key to exist
  condition_expression = "attribute_exists(#{@model_class.hash_key})"
  if @model_class.range_key?
    condition_expression += " AND attribute_exists(#{@model_class.range_key})"
  end
  builder.condition_expression = condition_expression

  builder.request
end

#observable_by_user_resultObject



41
42
43
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 41

def observable_by_user_result
  nil
end

#on_commitObject



29
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 29

def on_commit; end

#on_registrationObject



19
20
21
22
23
24
25
26
27
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 19

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

  if @block
    @item_updater = ItemUpdater.new(@model_class)
    @block.call(@item_updater)
  end
end

#on_rollbackObject



31
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 31

def on_rollback; end

#skipped?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 37

def skipped?
  @attributes.empty? && (!@item_updater || @item_updater.empty?)
end