Class: DynamoidAdvancedWhere::BatchedUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid_advanced_where/batched_updater.rb

Constant Summary collapse

DEEP_MERGE_ATTRIBUTES =
%i[expression_attribute_names expression_attribute_values].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_builder:) ⇒ BatchedUpdater

Returns a new instance of BatchedUpdater.



11
12
13
14
15
16
17
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 11

def initialize(query_builder:)
  self.query_builder = query_builder
  self._set_values = {}
  self._set_appends = []
  self._array_appends = []
  self._increments = Hash.new(0)
end

Instance Attribute Details

#_array_appendsObject

Returns the value of attribute _array_appends.



7
8
9
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 7

def _array_appends
  @_array_appends
end

#_incrementsObject

Returns the value of attribute _increments.



7
8
9
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 7

def _increments
  @_increments
end

#_set_appendsObject

Returns the value of attribute _set_appends.



7
8
9
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 7

def _set_appends
  @_set_appends
end

#_set_valuesObject

Returns the value of attribute _set_values.



7
8
9
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 7

def _set_values
  @_set_values
end

#query_builderObject

Returns the value of attribute query_builder.



7
8
9
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 7

def query_builder
  @query_builder
end

Instance Method Details

#append_to(appends) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 39

def append_to(appends)
  appends.each do |k, v|
    case klass.attributes[k.to_sym][:type]
    when :set
      _set_appends << { k => v.to_set }
    when :array
      _array_appends << { k => v }
    else
      raise 'can only append to sets or arrays'
    end
  end

  self
end

#apply(hash_key, range_key = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 19

def apply(hash_key, range_key = nil)
  key_args = {
    table_name: klass.table_name,
    return_values: 'ALL_NEW',
    key: {
      klass.hash_key => hash_key,
      klass.range_key => range_key,
    }.delete_if { |k, _v| k.nil? },
  }
  resp = client.update_item(update_item_arguments.merge(key_args))

  klass.from_database(resp.attributes)
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
end

#decrement(*fields, by: 1) ⇒ Object



59
60
61
62
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 59

def decrement(*fields, by: 1)
  increment(*fields, by: -1 * by)
  self
end

#increment(*fields, by: 1) ⇒ Object



54
55
56
57
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 54

def increment(*fields, by: 1)
  fields.each { |field| _increments[field] += by }
  self
end

#set_values(vals) ⇒ Object



34
35
36
37
# File 'lib/dynamoid_advanced_where/batched_updater.rb', line 34

def set_values(vals)
  _set_values.merge!(vals)
  self
end