Class: Dynamoid::Persistence::Inc

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid/persistence/inc.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, hash_key, range_key = nil, counters) ⇒ Inc

rubocop:disable Style/OptionalArguments



12
13
14
15
16
17
# File 'lib/dynamoid/persistence/inc.rb', line 12

def initialize(model_class, hash_key, range_key = nil, counters)
  @model_class = model_class
  @hash_key = hash_key
  @range_key = range_key
  @counters = counters
end

Class Method Details

.call(model_class, hash_key, range_key = nil, counters) ⇒ Object



7
8
9
# File 'lib/dynamoid/persistence/inc.rb', line 7

def self.call(model_class, hash_key, range_key = nil, counters)
  new(model_class, hash_key, range_key, counters).call
end

Instance Method Details

#callObject

rubocop:enable Style/OptionalArguments



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dynamoid/persistence/inc.rb', line 20

def call
  touch = @counters.delete(:touch)

  Dynamoid.adapter.update_item(@model_class.table_name, @hash_key, update_item_options) do |t|
    @counters.each do |name, value|
      t.add(name => cast_and_dump_attribute_value(name, value))
    end

    if touch
      value = DateTime.now.in_time_zone(Time.zone)

      timestamp_attributes_to_touch(touch).each do |name|
        t.set(name => cast_and_dump_attribute_value(name, value))
      end
    end
  end
end