Skip to content

Commit

Permalink
Refactor .inc and move code to a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Jan 8, 2023
1 parent 04fbc58 commit c697418
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 34 deletions.
36 changes: 2 additions & 34 deletions lib/dynamoid/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'dynamoid/persistence/update_fields'
require 'dynamoid/persistence/upsert'
require 'dynamoid/persistence/save'
require 'dynamoid/persistence/inc'
require 'dynamoid/persistence/update_validations'

# encoding: utf-8
Expand Down Expand Up @@ -392,40 +393,7 @@ def upsert(hash_key_value, range_key_value = nil, attrs = {}, conditions = {})
# @option counters [true | Symbol | Array[Symbol]] :touch to update update_at attribute and optionally the specified ones
# @return [Model class] self
def inc(hash_key_value, range_key_value = nil, counters)
options = if range_key
value_casted = TypeCasting.cast_field(range_key_value, attributes[range_key])
value_dumped = Dumping.dump_field(value_casted, attributes[range_key])
{ range_key: value_dumped }
else
{}
end

touch = counters.delete(:touch)

Dynamoid.adapter.update_item(table_name, hash_key_value, options) do |t|
counters.each do |k, v|
value_casted = TypeCasting.cast_field(v, attributes[k])
value_dumped = Dumping.dump_field(value_casted, attributes[k])

t.add(k => value_dumped)
end

if touch
names = []
names << :updated_at if timestamps_enabled?
names += Array.wrap(touch) if touch != true

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

names.each do |name|
value_casted = TypeCasting.cast_field(value, attributes[name])
value_dumped = Dumping.dump_field(value_casted, attributes[name])

t.set(name => value_dumped)
end
end
end

Inc.call(self, hash_key_value, range_key_value, counters)
self
end
end
Expand Down
66 changes: 66 additions & 0 deletions lib/dynamoid/persistence/inc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

module Dynamoid
module Persistence
# @private
class Inc
def self.call(model_class, hash_key, range_key = nil, counters)
new(model_class, hash_key, range_key, counters).call
end

# rubocop:disable Style/OptionalArguments
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
# rubocop:enable Style/OptionalArguments

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

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

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

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
end
end
end

0 comments on commit c697418

Please sign in to comment.