Skip to content

Commit 69b67e6

Browse files
authored
Feature/ruby 2.7 updates (#4)
* Remove Ruby 2.7 deprecation warnings related to keyword arguments * drop support for Ruby 2.2
1 parent 16214d5 commit 69b67e6

File tree

6 files changed

+127
-8
lines changed

6 files changed

+127
-8
lines changed

.travis.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
sudo: false
22
language: ruby
33
rvm:
4-
- 2.2.5
5-
- 2.3.1
6-
before_install: gem install bundler -v 1.13.1
4+
- 2.3
5+
- 2.4
6+
- 2.5
7+
- 2.6
8+
- 2.7
9+
before_install: gem install bundler -v 2.1.4

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# AddressFinder 1.0.3 (May 27, 2020)
2+
3+
* Add support for Ruby 2.7
4+
* Drop support for Ruby 2.2
5+
* Ruby 2.7 deprecated the use of hashes in the last argument of a method call. We added a check to handle that deprecation. Read more here: https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
6+
17
# AddressFinder 1.0.2 (November 22, 2016) #
28

39
* Raise a ValidationError when #perform! fails rather that call the Rails 5

lib/use_case_pattern/base.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Base
99
module ClassMethods
1010
# The perform method of a UseCase should always return itself
1111
def perform(*args)
12-
new(*args).tap do |use_case|
12+
use_case(*args).tap do |use_case|
1313
if use_case.valid?
1414
use_case.perform
1515
end
@@ -18,7 +18,20 @@ def perform(*args)
1818

1919
# Raise a validation error if perform has created any errors
2020
def perform!(*args)
21-
new(*args).tap { |use_case| use_case.perform! }
21+
use_case(*args).tap { |use_case| use_case.perform! }
22+
end
23+
24+
private
25+
26+
# initialises the use_case, making sure that any keyword arguments are
27+
# dealt with appropriately: https://piechowski.io/post/last-arg-keyword-deprecated-ruby-2-7/
28+
def use_case(*args)
29+
if args.last.is_a?(Hash)
30+
keyword_arguments = args.pop
31+
new(*args, **keyword_arguments)
32+
else
33+
new(*args)
34+
end
2235
end
2336
end
2437

lib/use_case_pattern/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module UseCasePattern
2-
VERSION = "1.0.2"
2+
VERSION = "1.0.3"
33
end

spec/use_case_pattern/base_spec.rb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,59 @@
3131
expect { UseCaseWithMissingPerformMethod.perform }.to raise_error(NotImplementedError)
3232
end
3333
end
34+
35+
context "with a class containing a constructor with hash arguments" do
36+
subject(:result){ UseCaseWithConstructorContainingHash.perform(name: "Bert", age: 5) }
37+
38+
it "should have no errors" do
39+
expect(result.errors.full_messages).to eq([])
40+
end
41+
42+
it "should return the use case object" do
43+
expect(result.class).to eq(UseCaseWithConstructorContainingHash)
44+
end
45+
46+
it "should have set the name and age" do
47+
expect(result.age).to eq(5)
48+
expect(result.name).to eq("Bert")
49+
end
50+
end
51+
52+
context "with a class containing a constructor with multiple arguments" do
53+
subject(:result){ UseCaseWithConstructorContainingStringAndHash.perform("Rocket", height: 50, width: 5) }
54+
55+
it "should have no errors" do
56+
expect(result.errors.full_messages).to eq([])
57+
end
58+
59+
it "should return the use case object" do
60+
expect(result.class).to eq(UseCaseWithConstructorContainingStringAndHash)
61+
end
62+
63+
it "should have set the name and age" do
64+
expect(result.label).to eq("Rocket")
65+
expect(result.height).to eq(50)
66+
expect(result.width).to eq(5)
67+
end
68+
end
69+
70+
context "with a class containing a constructor with multiple hashes" do
71+
subject(:result){ UseCaseWithConstructorContainingTwoHashes.perform({label: "Rocket"}, height: 50, width: 5) }
72+
73+
it "should have no errors" do
74+
expect(result.errors.full_messages).to eq([])
75+
end
76+
77+
it "should return the use case object" do
78+
expect(result.class).to eq(UseCaseWithConstructorContainingTwoHashes)
79+
end
80+
81+
it "should have set the name and age" do
82+
expect(result.label).to eq("Rocket")
83+
expect(result.height).to eq(50)
84+
expect(result.width).to eq(5)
85+
end
86+
end
3487
end
3588

3689
context "with a use case that generates errors" do
@@ -77,6 +130,50 @@ def perform
77130
end
78131
end
79132

133+
class UseCaseWithConstructorContainingHash
134+
include UseCasePattern
135+
136+
attr_reader :name, :age
137+
138+
def initialize(name:, age:)
139+
@name = name
140+
@age = age
141+
end
142+
143+
def perform
144+
end
145+
end
146+
147+
class UseCaseWithConstructorContainingTwoHashes
148+
include UseCasePattern
149+
150+
attr_reader :label, :height, :width
151+
152+
def initialize(my_hash, height:, width:)
153+
@label = my_hash[:label]
154+
@height = height
155+
@width = width
156+
end
157+
158+
def perform
159+
end
160+
end
161+
162+
class UseCaseWithConstructorContainingStringAndHash
163+
include UseCasePattern
164+
165+
attr_reader :label, :height, :width
166+
167+
def initialize(label, height:, width:)
168+
@label = label
169+
@height = height
170+
@width = width
171+
end
172+
173+
def perform
174+
end
175+
end
176+
80177
class UseCaseThatGeneratesErrors
81178
include UseCasePattern
82179

use_case_pattern.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ Gem::Specification.new do |spec|
2020
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2121
spec.require_paths = ["lib"]
2222

23-
spec.required_ruby_version = '~> 2.2'
23+
spec.required_ruby_version = '~> 2.3'
2424

2525
spec.add_runtime_dependency "activemodel", [">= 4.0.0"]
2626
spec.add_runtime_dependency "activesupport", [">= 4.0.0"]
2727

28-
spec.add_development_dependency "bundler", "~> 1.13"
28+
spec.add_development_dependency "bundler", "~> 2.1.4"
2929
spec.add_development_dependency "rake", "~> 10.0"
3030
spec.add_development_dependency "rspec", "~> 3.0"
3131
end

0 commit comments

Comments
 (0)