Skip to content

Commit

Permalink
Fix Rubocop warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Nov 3, 2024
1 parent 4b28635 commit 7a35e94
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 182 deletions.
19 changes: 9 additions & 10 deletions lib/dynamoid/transaction_write.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TransactionWrite
attr_reader :actions

def self.execute
transaction = self.new
transaction = new
yield transaction
transaction.commit
end
Expand Down Expand Up @@ -68,12 +68,12 @@ def create(model_class, attributes = {}, &block)
end
end

def upsert(model_class, hash_key, range_key = nil, attributes)
def upsert(model_class, hash_key, range_key = nil, attributes) # rubocop:disable Style/OptionalArguments
action = Dynamoid::TransactionWrite::Upsert.new(model_class, hash_key, range_key, attributes)
register_action action
end

def update_fields(model_class, hash_key, range_key = nil, attributes)
def update_fields(model_class, hash_key, range_key = nil, attributes) # rubocop:disable Style/OptionalArguments
action = Dynamoid::TransactionWrite::UpdateFields.new(model_class, hash_key, range_key, attributes)
register_action action
end
Expand All @@ -89,13 +89,12 @@ def update_attributes!(model, attributes)
end

def delete(model_or_model_class, hash_key = nil, range_key = nil)
if model_or_model_class.is_a? Class
action = Dynamoid::TransactionWrite::DeleteWithPrimaryKey.new(model_or_model_class, hash_key, range_key)
register_action action
else
action = Dynamoid::TransactionWrite::DeleteWithInstance.new(model_or_model_class)
register_action action
end
action = if model_or_model_class.is_a? Class
Dynamoid::TransactionWrite::DeleteWithPrimaryKey.new(model_or_model_class, hash_key, range_key)
else
Dynamoid::TransactionWrite::DeleteWithInstance.new(model_or_model_class)
end
register_action action
end

def destroy!(model)
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/transaction_write/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(model_class, attributes = {}, **options, &block)
@model = model_class.new(attributes)

if block
block.call(@model)
yield(@model)
end

@save_action = Save.new(@model, **options)
Expand Down
3 changes: 1 addition & 2 deletions lib/dynamoid/transaction_write/delete_with_primary_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def on_registration
validate_primary_key!
end

def on_completing
end
def on_completing; end

def aborted?
false
Expand Down
5 changes: 3 additions & 2 deletions lib/dynamoid/transaction_write/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def on_registration

def on_completing
return if @aborted

@model.destroyed = true
end

Expand All @@ -39,19 +40,19 @@ def skip?

def observable_by_user_result
return false if @aborted

@model
end

def action_request
key = { @model_class.hash_key => @model.hash_key }
key[@model_class.range_key] = @model.range_value if @model_class.range_key?
result = {
{
delete: {
key: key,
table_name: @model_class.table_name
}
}
result
end

private
Expand Down
16 changes: 6 additions & 10 deletions lib/dynamoid/transaction_write/save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ def initialize(model, **options)
def on_registration
validate_model!

unless @options[:validate] == false
unless @valid = @model.valid?
if @options[:raise_error]
raise Dynamoid::Errors::DocumentNotValid, @model
else
@aborted = true
return
end
if @options[:validate] != false && !(@valid = @model.valid?)
if @options[:raise_error]
raise Dynamoid::Errors::DocumentNotValid, @model
else
@aborted = true
return
end
end

Expand Down Expand Up @@ -143,8 +141,6 @@ def action_request_to_update
result
end

private

def touch_model_timestamps(skip_created_at:)
return unless @model_class.timestamps_enabled?

Expand Down
3 changes: 1 addition & 2 deletions lib/dynamoid/transaction_write/update_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def on_registration
validate_primary_key!
end

def on_completing
end
def on_completing; end

def aborted?
false
Expand Down
7 changes: 2 additions & 5 deletions lib/dynamoid/transaction_write/upsert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def on_registration
validate_primary_key!
end

def on_completing
end
def on_completing; end

def aborted?
false
Expand Down Expand Up @@ -54,7 +53,7 @@ def action_request
# expression_attribute_names = attribute_keys_in_model.map{|k| ["##{k}","#{k}"]}.to_h
expression_attribute_names.merge!(item_keys.each_with_index.map { |k, i| ["#_n#{i}", k.to_s] }.to_h)

result = {
{
update: {
key: key,
table_name: @model_class.table_name,
Expand All @@ -63,8 +62,6 @@ def action_request
expression_attribute_values: expression_attribute_values
}
}

result
end

private
Expand Down
24 changes: 12 additions & 12 deletions spec/dynamoid/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -871,17 +871,17 @@ def around_save_callback
end

klass_with_callbacks.create!
expect(ScratchPad.recorded).to eql [ # rubocop:disable Style/StringConcatenation
'run before_validation',
'run after_validation',
'run before_save',
'start around_save',
'run before_create',
'start around_create',
'finish around_create',
'run after_create',
'finish around_save',
'run after_save'
expect(ScratchPad.recorded).to eql [
'run before_validation',
'run after_validation',
'run before_save',
'start around_save',
'run before_create',
'start around_create',
'finish around_create',
'run after_create',
'finish around_save',
'run after_save'
]
end
end
Expand Down Expand Up @@ -2010,7 +2010,7 @@ def around_update_callback
}.to('[Updated]')
end

# TODO:
# TODO: implement the test later
# it 'raises ...Error when range key is missing'

# TODO: add this case for save/save! and update_attributes/other update operations
Expand Down
2 changes: 1 addition & 1 deletion spec/dynamoid/transaction_write/commit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
transaction.create klass
transaction.create klass

expect { transaction.commit }.to change { klass.count }.by(2)
expect { transaction.commit }.to change(klass, :count).by(2)
end
end
Loading

0 comments on commit 7a35e94

Please sign in to comment.