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



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

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



9
10
11
# File 'lib/dynamoid/persistence/inc.rb', line 9

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



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

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

  Dynamoid.adapter.update_item(@model_class.table_name, @hash_key, update_item_options) do |t|
    item_updater = ItemUpdaterWithCastingAndDumping.new(@model_class, t)

    @counters.each do |name, value|
      item_updater.add(name => value)
    end

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

      timestamp_attributes_to_touch(touch).each do |name|
        item_updater.set(name => value)
      end
    end
  end
end