Skip to content

Commit 4e8e9da

Browse files
committed
Support Ruby 3.0. Fix errors related to the keyword arguments changes
1 parent 497d8b3 commit 4e8e9da

File tree

9 files changed

+38
-11
lines changed

9 files changed

+38
-11
lines changed

lib/dynamoid/adapter.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,14 @@ def delete_table(table_name, options = {})
158158
#
159159
# @since 0.2.0
160160
def method_missing(method, *args, &block)
161-
return benchmark(method, *args) { adapter.send(method, *args, &block) } if adapter.respond_to?(method)
161+
# Don't use keywork arguments delegating (with **kw). It works in
162+
# different way in different Ruby versions: <= 2.6, 2.7, 3.0 and in some
163+
# future 3.x versions. Providing that there are no downstream methods
164+
# with keyword arguments in adapter.
165+
#
166+
# https://eregon.me/blog/2019/11/10/the-delegation-challenge-of-ruby27.html
162167

168+
return benchmark(method, *args) { adapter.send(method, *args, &block) } if adapter.respond_to?(method)
163169
super
164170
end
165171

lib/dynamoid/adapter_plugin/aws_sdk_v3.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ module Dynamoid
1212
# @private
1313
module AdapterPlugin
1414
# The AwsSdkV3 adapter provides support for the aws-sdk version 2 for ruby.
15+
16+
# Note: Don't use keyword arguments in public methods as far as method
17+
# calls on adapter are delegated to the plugin.
18+
#
19+
# There are breaking changes in Ruby related to delegating keyword
20+
# arguments so we have decided just to avoid them when use delegation.
21+
#
22+
# https://eregon.me/blog/2019/11/10/the-delegation-challenge-of-ruby27.html
23+
1524
class AwsSdkV3
1625
EQ = 'EQ'
1726
RANGE_MAP = {
@@ -281,7 +290,7 @@ def create_table(table_name, key = :id, options = {})
281290
false
282291
end
283292

284-
def update_time_to_live(table_name:, attribute:)
293+
def update_time_to_live(table_name, attribute)
285294
request = {
286295
table_name: table_name,
287296
time_to_live_specification: {

lib/dynamoid/criteria.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ module ClassMethods
1515
#
1616
# @since 0.2.0
1717
define_method(meth) do |*args, &blk|
18+
# Don't use keywork arguments delegating (with **kw). It works in
19+
# different way in different Ruby versions: <= 2.6, 2.7, 3.0 and in some
20+
# future 3.x versions. Providing that there are no downstream methods
21+
# with keyword arguments in Chain.
22+
#
23+
# https://eregon.me/blog/2019/11/10/the-delegation-challenge-of-ruby27.html
24+
1825
chain = Dynamoid::Criteria::Chain.new(self)
1926
if args
2027
chain.send(meth, *args, &blk)

lib/dynamoid/loadable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def reload
2323
options[:range_key] = range_value
2424
end
2525

26-
self.attributes = self.class.find(hash_key, options).attributes
26+
self.attributes = self.class.find(hash_key, **options).attributes
2727
@associations.values.each(&:reset)
2828
self
2929
end

lib/dynamoid/persistence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def create_table(options = {})
112112

113113
if created_successfuly && self.options[:expires]
114114
attribute = self.options[:expires][:field]
115-
Dynamoid.adapter.update_time_to_live(table_name: table_name, attribute: attribute)
115+
Dynamoid.adapter.update_time_to_live(table_name, attribute)
116116
end
117117
end
118118

lib/dynamoid/persistence/update_fields.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module Dynamoid
44
module Persistence
55
# @private
66
class UpdateFields
7-
def self.call(*args)
8-
new(*args).call
7+
def self.call(*args, **options)
8+
new(*args, **options).call
99
end
1010

1111
def initialize(model_class, partition_key:, sort_key:, attributes:, conditions:)

lib/dynamoid/persistence/upsert.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module Dynamoid
44
module Persistence
55
# @private
66
class Upsert
7-
def self.call(*args)
8-
new(*args).call
7+
def self.call(*args, **options)
8+
new(*args, **options).call
99
end
1010

1111
def initialize(model_class, partition_key:, sort_key:, attributes:, conditions:)

spec/dynamoid/adapter_plugin/aws_sdk_v3_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,11 +1224,11 @@ def dynamo_request(table_name, scan_hash = {}, select_opts = {})
12241224
)
12251225
.and_call_original
12261226

1227-
Dynamoid.adapter.update_time_to_live(table_name: table_name, attribute: :ttl)
1227+
Dynamoid.adapter.update_time_to_live(table_name, :ttl)
12281228
end
12291229

12301230
it 'updates a table schema' do
1231-
Dynamoid.adapter.update_time_to_live(table_name: table_name, attribute: :ttl)
1231+
Dynamoid.adapter.update_time_to_live(table_name, :ttl)
12321232

12331233
response = Dynamoid.adapter.client.describe_time_to_live(table_name: table_name)
12341234
expect(response.time_to_live_description.time_to_live_status).to eq 'ENABLED'

spec/dynamoid/persistence_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,13 @@ def self.dynamoid_field_type
416416
end
417417

418418
it 'sets up TTL for table' do
419+
# Run the spec when a fix in rspec-mock is released
420+
# - https://github.com/rspec/rspec-mocks/issues/1306
421+
# - https://github.com/rspec/rspec-mocks/pull/1385
422+
skip "There is an issue with Ruby 3.0 and rspec-mocks related to keyword arguments"
423+
419424
expect(Dynamoid.adapter).to receive(:update_time_to_live)
420-
.with(table_name: class_with_expiration.table_name, attribute: :ttl)
425+
.with(class_with_expiration.table_name, :ttl)
421426
.and_call_original
422427

423428
class_with_expiration.create_table

0 commit comments

Comments
 (0)