class Dynamoid::Persistence::Inc

@private

Public Class Methods

call(model_class, hash_key, range_key = nil, counters) click to toggle source
# 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
new(model_class, hash_key, range_key = nil, counters) click to toggle source

rubocop:disable Style/OptionalArguments

# 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

Public Instance Methods

call() click to toggle source

rubocop:enable Style/OptionalArguments

# 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

Private Instance Methods

cast_and_dump_attribute_value(name, value) click to toggle source
# File lib/dynamoid/persistence/inc.rb, line 51
def cast_and_dump_attribute_value(name, value)
  value_casted = TypeCasting.cast_field(value, @model_class.attributes[name])
  Dumping.dump_field(value_casted, @model_class.attributes[name])
end
timestamp_attributes_to_touch(touch) click to toggle source
# File lib/dynamoid/persistence/inc.rb, line 56
def timestamp_attributes_to_touch(touch)
  return [] unless touch

  names = []
  names << :updated_at if @model_class.timestamps_enabled?
  names += Array.wrap(touch) if touch != true
  names
end
update_item_options() click to toggle source
# File lib/dynamoid/persistence/inc.rb, line 40
def update_item_options
  if @model_class.range_key
    range_key_options = @model_class.attributes[@model_class.range_key]
    value_casted = TypeCasting.cast_field(@range_key, range_key_options)
    value_dumped = Dumping.dump_field(value_casted, range_key_options)
    { range_key: value_dumped }
  else
    {}
  end
end