in lib/aws-record/record/attributes.rb [474:507]
def atomic_counter(name, opts = {})
opts[:dynamodb_type] = 'N'
opts[:default_value] ||= 0
attr(name, Marshalers::IntegerMarshaler.new(opts), opts)
define_method("increment_#{name}!") do |increment = 1|
if dirty?
msg = 'Attributes need to be saved before atomic counter can be incremented'
raise Errors::RecordError, msg
end
unless increment.is_a?(Integer)
msg = "expected an Integer value, got #{increment.class}"
raise ArgumentError, msg
end
resp = dynamodb_client.update_item(
table_name: self.class.table_name,
key: key_values,
expression_attribute_values: {
':i' => increment
},
expression_attribute_names: {
'#n' => name
},
update_expression: 'SET #n = #n + :i',
return_values: 'UPDATED_NEW'
)
assign_attributes(resp[:attributes])
@data.clean!
@data.get_attribute(name)
end
end