Skip to content

Commit

Permalink
Fix Ruby 2.7 keyword argument deprecation warnings
Browse files Browse the repository at this point in the history
Serveral methods had been written in a way that calling them resulted in the
following deprecation warning when using Dynamoid with Ruby 2.7:

warning: Using the last argument as keyword parameters is deprecated; maybe **
should be added to the call

This change rectifies this by explicitly using keyword arguments instead of
implicitly relying on Ruby to do the right thing.
  • Loading branch information
geek4good committed Jun 12, 2020
1 parent d136cc2 commit 8216c69
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
10 changes: 8 additions & 2 deletions lib/dynamoid/adapter.rb
Expand Up @@ -155,9 +155,15 @@ def delete_table(table_name, options = {})
#
# @since 0.2.0
def method_missing(method, *args, &block)
return benchmark(method, *args) { adapter.send(method, *args, &block) } if adapter.respond_to?(method)
super unless adapter.respond_to?(method)

super
if args.last.is_a?(Hash)
return benchmark(method, *args[0..-2], **args.last) do
adapter.send(method, *args[0..-2], **args.last, &block)
end
end

benchmark(method, *args) { adapter.send(method, *args, &block) }
end

# Query the DynamoDB table. This employs DynamoDB's indexes so is generally faster than scanning, but is
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/loadable.rb
Expand Up @@ -23,7 +23,7 @@ def reload
options[:range_key] = range_value
end

self.attributes = self.class.find(hash_key, options).attributes
self.attributes = self.class.find(hash_key, **options).attributes
@associations.values.each(&:reset)
self
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/persistence/update_fields.rb
Expand Up @@ -4,7 +4,7 @@ module Dynamoid
module Persistence
class UpdateFields
def self.call(*args)
new(*args).call
new(args.first, **args.last).call
end

def initialize(model_class, partition_key:, sort_key:, attributes:, conditions:)
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/persistence/upsert.rb
Expand Up @@ -4,7 +4,7 @@ module Dynamoid
module Persistence
class Upsert
def self.call(*args)
new(*args).call
new(args.first, **args.last).call
end

def initialize(model_class, partition_key:, sort_key:, attributes:, conditions:)
Expand Down
4 changes: 4 additions & 0 deletions spec/dynamoid/document_spec.rb
Expand Up @@ -339,6 +339,10 @@ def city=(value)
end
end

it 'does not output a deprecation warning' do
expect { model.create }.to_not output(/is deprecated/).to_stderr
end

it 'sets default value at the updating' do
obj = model.create

Expand Down
15 changes: 15 additions & 0 deletions spec/dynamoid/persistence_spec.rb
Expand Up @@ -1118,6 +1118,14 @@ def log_message
end
end

it 'does not output a deprecation warning' do
document_class.create_table

expect do
document_class.update_fields('some-fake-id', title: 'Title')
end.not_to output(/is deprecated/).to_stderr
end

it 'does not create new document if it does not exist yet' do
document_class.create_table

Expand Down Expand Up @@ -1245,6 +1253,13 @@ def log_message
end
end

it 'does not output a deprecation warning' do
obj = document_class.create(title: 'Old title')
expect do
document_class.upsert(obj.id, title: 'New title')
end.to_not output(/is deprecated/).to_stderr
end

it 'changes field value' do
obj = document_class.create(title: 'Old title')
expect do
Expand Down

0 comments on commit 8216c69

Please sign in to comment.