f_service 0.2.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/.dependabot.yml +8 -0
- data/.github/workflows/tests-and-linter.yml +1 -1
- data/.rubocop.yml +19 -0
- data/CHANGELOG.md +19 -1
- data/Gemfile +3 -1
- data/Gemfile.lock +66 -38
- data/README.md +91 -8
- data/f_service.gemspec +3 -4
- data/lib/f_service/base.rb +37 -34
- data/lib/f_service/result/base.rb +70 -38
- data/lib/f_service/result/failure.rb +11 -7
- data/lib/f_service/result/success.rb +13 -8
- data/lib/f_service/rspec/support/helpers/result.rb +32 -0
- data/lib/f_service/rspec/support/helpers.rb +3 -0
- data/lib/f_service/rspec/support/matchers/result.rb +61 -0
- data/lib/f_service/rspec/support/matchers.rb +3 -0
- data/lib/f_service/rspec/support.rb +4 -0
- data/lib/f_service/rspec.rb +3 -0
- data/lib/f_service/version.rb +1 -1
- data/lib/f_service.rb +19 -4
- metadata +14 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4997e65a2e3054de3de7a252a3e85143d41701f44046982ffc583c486755aa1d
|
4
|
+
data.tar.gz: ea5212762e5529ec315dac3437d29ac75a5c20058d30fdc382fe31b6f18288a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3c51bf4006bb0b037361690fabb7e0af7895b393394561ed9a784d89ffe063bd48e573406fbee6675e5d3ef5f9ca3f9085240c6855ece5a7a50259dd6264e98
|
7
|
+
data.tar.gz: 7af0dfcf8f92fb8ac39b43efc044c5f623c9c52084b0b1a9542a9d6f1902dbe7444eb5b3b00ec043302dfe9b6e78eb879bc1cb335c2e078930bb15c49a9fe176
|
data/.rubocop.yml
CHANGED
@@ -2,6 +2,7 @@ require:
|
|
2
2
|
- rubocop-rspec
|
3
3
|
|
4
4
|
AllCops:
|
5
|
+
TargetRubyVersion: 3.0.0
|
5
6
|
NewCops: enable
|
6
7
|
|
7
8
|
Layout/LineLength:
|
@@ -17,3 +18,21 @@ Style/DocumentationMethod:
|
|
17
18
|
Naming/MethodName:
|
18
19
|
Exclude:
|
19
20
|
- lib/f_service/base.rb
|
21
|
+
|
22
|
+
RSpec/ContextWording:
|
23
|
+
Prefixes:
|
24
|
+
- and
|
25
|
+
- but
|
26
|
+
- when
|
27
|
+
- with
|
28
|
+
- without
|
29
|
+
|
30
|
+
RSpec/ExampleLength:
|
31
|
+
Max: 20
|
32
|
+
|
33
|
+
RSpec/NestedGroups:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
##### RUBYGEMS #####
|
37
|
+
Gemspec/RequireMFA:
|
38
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
@@ -10,9 +10,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
10
10
|
<!-- ### Removed -->
|
11
11
|
---
|
12
12
|
|
13
|
+
## 0.3.0
|
14
|
+
### Added
|
15
|
+
- Drop Support to Ruby 2.6 and 2.7
|
16
|
+
- Add Support to Ruby 3.2 and 3.3
|
17
|
+
- Changed and_error to use a matcher instead of equality comparation #51
|
18
|
+
|
19
|
+
## 0.3.0
|
20
|
+
### Added
|
21
|
+
- Added Rspec Helper `#mock_service` #41;
|
22
|
+
- Added Rspec Matcher `#have_succeed_with` and `#have_failed_with` #41;
|
23
|
+
- Added `Success()`, `Failure()`, `Check()`, `Try()` now can be multipe types #41;
|
24
|
+
- Changed Depreacate `Result#type` method #41;
|
25
|
+
- Changed Deprecate method `#then` for Success and Failure classes #40
|
26
|
+
- Added RSpec support for mock and match results #35
|
27
|
+
- Deprecate method `#then` for Success and Failure classes #40;
|
28
|
+
- Removed deprecated method `#on` #33
|
29
|
+
- Changed Capture just one callback per result #30;
|
30
|
+
|
13
31
|
## 0.2.0
|
14
32
|
### Added
|
15
|
-
- Add `
|
33
|
+
- Add `and_then` as alias for `then` (partially fix [#23](https://github.com/Fretadao/f_service/issues/23)).
|
16
34
|
- Add `catch` to `Failure` and `Success`. It acts as an inverted `then` and has a `or_else` alias.
|
17
35
|
- Add support to custom `data` property to be passed when calling `Base#Check`.
|
18
36
|
- Add support to multiple type checks on `Result#on_success` and `Result#on_failure` hooks.
|
data/Gemfile
CHANGED
@@ -6,8 +6,10 @@ source 'https://rubygems.org'
|
|
6
6
|
gemspec
|
7
7
|
|
8
8
|
group :development, :test do
|
9
|
+
gem 'pry'
|
10
|
+
gem 'pry-nav'
|
9
11
|
gem 'rake', '~> 13.0.0'
|
10
|
-
gem 'rubocop', '~>
|
12
|
+
gem 'rubocop', '~> 1.60.2', require: false
|
11
13
|
gem 'rubocop-rspec', require: false
|
12
14
|
end
|
13
15
|
|
data/Gemfile.lock
CHANGED
@@ -1,32 +1,47 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
f_service (0.
|
4
|
+
f_service (0.3.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
ast (2.4.
|
10
|
-
backport (1.
|
11
|
-
benchmark (0.
|
9
|
+
ast (2.4.2)
|
10
|
+
backport (1.2.0)
|
11
|
+
benchmark (0.3.0)
|
12
|
+
coderay (1.1.3)
|
12
13
|
diff-lcs (1.3)
|
13
14
|
docile (1.3.5)
|
14
15
|
e2mmap (0.1.0)
|
15
|
-
jaro_winkler (1.5.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
jaro_winkler (1.5.6)
|
17
|
+
json (2.7.1)
|
18
|
+
kramdown (2.4.0)
|
19
|
+
rexml
|
20
|
+
kramdown-parser-gfm (1.1.0)
|
21
|
+
kramdown (~> 2.0)
|
22
|
+
language_server-protocol (3.17.0.3)
|
23
|
+
method_source (1.0.0)
|
24
|
+
mini_portile2 (2.8.6)
|
25
|
+
nokogiri (1.16.5)
|
26
|
+
mini_portile2 (~> 2.8.2)
|
20
27
|
racc (~> 1.4)
|
21
|
-
parallel (1.
|
22
|
-
parser (
|
23
|
-
ast (~> 2.4.
|
24
|
-
|
25
|
-
|
28
|
+
parallel (1.24.0)
|
29
|
+
parser (3.3.0.5)
|
30
|
+
ast (~> 2.4.1)
|
31
|
+
racc
|
32
|
+
pry (0.14.1)
|
33
|
+
coderay (~> 1.1)
|
34
|
+
method_source (~> 1.0)
|
35
|
+
pry-nav (1.0.0)
|
36
|
+
pry (>= 0.9.10, < 0.15)
|
37
|
+
racc (1.7.3)
|
38
|
+
rainbow (3.1.1)
|
26
39
|
rake (13.0.1)
|
27
|
-
|
40
|
+
regexp_parser (2.9.0)
|
41
|
+
reverse_markdown (2.1.1)
|
28
42
|
nokogiri
|
29
|
-
rexml (3.2.
|
43
|
+
rexml (3.2.8)
|
44
|
+
strscan (>= 3.0.9)
|
30
45
|
rspec (3.9.0)
|
31
46
|
rspec-core (~> 3.9.0)
|
32
47
|
rspec-expectations (~> 3.9.0)
|
@@ -40,55 +55,68 @@ GEM
|
|
40
55
|
diff-lcs (>= 1.2.0, < 2.0)
|
41
56
|
rspec-support (~> 3.9.0)
|
42
57
|
rspec-support (3.9.2)
|
43
|
-
rubocop (
|
44
|
-
|
58
|
+
rubocop (1.60.2)
|
59
|
+
json (~> 2.3)
|
60
|
+
language_server-protocol (>= 3.17.0)
|
45
61
|
parallel (~> 1.10)
|
46
|
-
parser (>=
|
62
|
+
parser (>= 3.3.0.2)
|
47
63
|
rainbow (>= 2.2.2, < 4.0)
|
48
|
-
|
64
|
+
regexp_parser (>= 1.8, < 3.0)
|
65
|
+
rexml (>= 3.2.5, < 4.0)
|
66
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
49
67
|
ruby-progressbar (~> 1.7)
|
50
|
-
unicode-display_width (>=
|
51
|
-
rubocop-
|
52
|
-
|
53
|
-
|
68
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
69
|
+
rubocop-ast (1.30.0)
|
70
|
+
parser (>= 3.2.1.0)
|
71
|
+
rubocop-capybara (2.20.0)
|
72
|
+
rubocop (~> 1.41)
|
73
|
+
rubocop-factory_bot (2.25.1)
|
74
|
+
rubocop (~> 1.41)
|
75
|
+
rubocop-rspec (2.26.1)
|
76
|
+
rubocop (~> 1.40)
|
77
|
+
rubocop-capybara (~> 2.17)
|
78
|
+
rubocop-factory_bot (~> 2.22)
|
79
|
+
ruby-progressbar (1.13.0)
|
54
80
|
simplecov (0.21.2)
|
55
81
|
docile (~> 1.1)
|
56
82
|
simplecov-html (~> 0.11)
|
57
83
|
simplecov_json_formatter (~> 0.1)
|
58
84
|
simplecov-html (0.12.3)
|
59
85
|
simplecov_json_formatter (0.1.2)
|
60
|
-
solargraph (0.
|
86
|
+
solargraph (0.41.2)
|
61
87
|
backport (~> 1.1)
|
62
88
|
benchmark
|
63
89
|
bundler (>= 1.17.2)
|
64
90
|
e2mmap
|
65
91
|
jaro_winkler (~> 1.5)
|
66
|
-
|
67
|
-
|
68
|
-
parser (~>
|
69
|
-
reverse_markdown (
|
70
|
-
rubocop (
|
92
|
+
kramdown (~> 2.3)
|
93
|
+
kramdown-parser-gfm (~> 1.1)
|
94
|
+
parser (~> 3.0)
|
95
|
+
reverse_markdown (>= 1.0.5, < 3)
|
96
|
+
rubocop (>= 0.52)
|
71
97
|
thor (~> 1.0)
|
72
98
|
tilt (~> 2.0)
|
73
|
-
yard (~> 0.9)
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
99
|
+
yard (~> 0.9, >= 0.9.24)
|
100
|
+
strscan (3.1.0)
|
101
|
+
thor (1.3.0)
|
102
|
+
tilt (2.3.0)
|
103
|
+
unicode-display_width (2.5.0)
|
104
|
+
yard (0.9.36)
|
78
105
|
|
79
106
|
PLATFORMS
|
80
107
|
ruby
|
81
108
|
|
82
109
|
DEPENDENCIES
|
83
|
-
bundler (~> 2.0)
|
84
110
|
f_service!
|
111
|
+
pry
|
112
|
+
pry-nav
|
85
113
|
rake (~> 13.0.0)
|
86
114
|
rspec (~> 3.0)
|
87
|
-
rubocop (~>
|
115
|
+
rubocop (~> 1.60.2)
|
88
116
|
rubocop-rspec
|
89
117
|
simplecov
|
90
118
|
solargraph
|
91
119
|
yard
|
92
120
|
|
93
121
|
BUNDLED WITH
|
94
|
-
2.
|
122
|
+
2.2.32
|
data/README.md
CHANGED
@@ -58,17 +58,17 @@ end
|
|
58
58
|
|
59
59
|
The next step is writing the `#run` method, which is where the work should be done.
|
60
60
|
Use the methods `#Success` and `#Failure` to handle your return values.
|
61
|
-
You can optionally specify a
|
61
|
+
You can optionally specify a list of types which represents that result and a value for your result.
|
62
62
|
|
63
63
|
```ruby
|
64
64
|
class User::Create < FService::Base
|
65
65
|
# ...
|
66
66
|
def run
|
67
|
-
return Failure(:no_name) if @name.nil?
|
67
|
+
return Failure(:no_name, :invalid_attribute) if @name.nil?
|
68
68
|
|
69
69
|
user = UserRepository.create(name: @name)
|
70
|
-
if user.
|
71
|
-
Success(:created, data: user)
|
70
|
+
if user.save
|
71
|
+
Success(:success, :created, data: user)
|
72
72
|
else
|
73
73
|
Failure(:creation_failed, data: user.errors)
|
74
74
|
end
|
@@ -128,6 +128,29 @@ class UsersController < BaseController
|
|
128
128
|
end
|
129
129
|
```
|
130
130
|
|
131
|
+
Or else it is possible to specify an unhandled option to ensure that the callback will process that message anyway the
|
132
|
+
error.
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
class UsersController < BaseController
|
136
|
+
def create
|
137
|
+
User::Create.(user_params)
|
138
|
+
.on_success(unhandled: true) { |value| return json_success(value) }
|
139
|
+
.on_failure(unhandled: true) { |error| return json_error(error) }
|
140
|
+
end
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
class UsersController < BaseController
|
146
|
+
def create
|
147
|
+
User::Create.(user_params)
|
148
|
+
.on_success { |value| return json_success(value) }
|
149
|
+
.on_failure { |error| return json_error(error) }
|
150
|
+
end
|
151
|
+
end
|
152
|
+
```
|
153
|
+
|
131
154
|
> You can ignore any of the callbacks, if you want to.
|
132
155
|
|
133
156
|
Going further, you can match the Result type, in case you want to handle them differently:
|
@@ -166,6 +189,15 @@ class UsersController < BaseController
|
|
166
189
|
end
|
167
190
|
```
|
168
191
|
|
192
|
+
### Type precedence
|
193
|
+
|
194
|
+
FService matches the service's types from left to right, from more specific to more generic.
|
195
|
+
For example, the following result `Failure(:unprocessable_entity, :client_error, :http_response)` will match in the following order:
|
196
|
+
1. `:unprocessable_entity`;
|
197
|
+
2. `:client_error`;
|
198
|
+
3. `:http_response`;
|
199
|
+
4. unmatched block;
|
200
|
+
|
169
201
|
### Chaining services
|
170
202
|
|
171
203
|
Since all services return Results, you can chain service calls making a data pipeline.
|
@@ -206,10 +238,10 @@ You can use `Check` to converts a boolean to a Result, truthy values map to `Suc
|
|
206
238
|
|
207
239
|
```ruby
|
208
240
|
Check(:math_works) { 1 < 2 }
|
209
|
-
# => #<Success @value=true, @
|
241
|
+
# => #<Success @value=true, @types=[:math_works]>
|
210
242
|
|
211
243
|
Check(:math_works) { 1 > 2 }
|
212
|
-
# => #<Failure @error=false, @
|
244
|
+
# => #<Failure @error=false, @types=[:math_works]>
|
213
245
|
```
|
214
246
|
|
215
247
|
`Try` transforms an exception into a `Failure` if some exception is raised for the given block. You can specify which exception class to watch for
|
@@ -228,10 +260,61 @@ class IHateEvenNumbers < FService::Base
|
|
228
260
|
end
|
229
261
|
|
230
262
|
IHateEvenNumbers.call
|
231
|
-
# => #<Success @value=9, @
|
263
|
+
# => #<Success @value=9, @types=[:rand_int]>
|
232
264
|
|
233
265
|
IHateEvenNumbers.call
|
234
|
-
# => #<Failure @error=#<RuntimeError: Yuck! It's a 4>, @
|
266
|
+
# => #<Failure @error=#<RuntimeError: Yuck! It's a 4>, @types=[:rand_int]>
|
267
|
+
```
|
268
|
+
|
269
|
+
## Testing
|
270
|
+
|
271
|
+
We provide some helpers and matchers to make ease to test code envolving Fservice services.
|
272
|
+
|
273
|
+
To make available in the system, in the file 'spec/spec_helper.rb' or 'spec/rails_helper.rb'
|
274
|
+
|
275
|
+
add the folowing require:
|
276
|
+
|
277
|
+
```rb
|
278
|
+
require 'f_service/rspec'
|
279
|
+
```
|
280
|
+
|
281
|
+
### Mocking a result
|
282
|
+
|
283
|
+
```rb
|
284
|
+
mock_service(Uer::Create)
|
285
|
+
# => Mocks a successful result with all values nil
|
286
|
+
|
287
|
+
mock_service(Uer::Create, result: :success)
|
288
|
+
# => Mocks a successful result with all values nil
|
289
|
+
|
290
|
+
mock_service(Uer::Create, result: :success, types: [:created, :success])
|
291
|
+
# => Mocks a successful result with type created
|
292
|
+
|
293
|
+
mock_service(Uer::Create, result: :success, types: :created, value: instance_spy(User))
|
294
|
+
# => Mocks a successful result with type created and a value
|
295
|
+
|
296
|
+
mock_service(Uer::Create, result: :failure)
|
297
|
+
# => Mocs a failure with all nil values
|
298
|
+
|
299
|
+
mock_service(User::Create, result: :failure, types: [:unprocessable_entity, :client_error])
|
300
|
+
# => Mocs a failure with a failure type
|
301
|
+
|
302
|
+
mock_service(User::Create, result: :failure, types: [:unprocessable_entity, :client_error], value: { name: ["can't be blank"] })
|
303
|
+
# => Mocs a failure with a failure type and an error value
|
304
|
+
```
|
305
|
+
|
306
|
+
### Matching a result
|
307
|
+
|
308
|
+
```rb
|
309
|
+
expect(User::Create.(name: 'Joe')).to have_succeed_with(:created)
|
310
|
+
|
311
|
+
expect(User::Create.(name: 'Joe')).to have_succeed_with(:created).and_value(an_instance_of(User))
|
312
|
+
|
313
|
+
expect(User::Create.(name: nil)).to have_failed_with(:invalid_attributes)
|
314
|
+
|
315
|
+
expect(User::Create.(name: nil)).to have_failed_with(:invalid_attributes).and_error({ name: ["can't be blank"] })
|
316
|
+
|
317
|
+
expect(User::Create.(name: nil)).to have_failed_with(:invalid_attributes).and_error(a_hash_including(name: ["can't be blank"]))
|
235
318
|
```
|
236
319
|
|
237
320
|
## API Docs
|
data/f_service.gemspec
CHANGED
@@ -7,8 +7,8 @@ require 'f_service/version'
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
8
|
spec.name = 'f_service'
|
9
9
|
spec.version = FService::VERSION
|
10
|
-
spec.authors = ['
|
11
|
-
spec.email
|
10
|
+
spec.authors = ['Fretadao Tech Team']
|
11
|
+
spec.email = ['tech@fretadao.com.br']
|
12
12
|
|
13
13
|
spec.summary = 'A small, monad-based service class'
|
14
14
|
spec.description = <<-DESCRIPTION
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.homepage = 'https://github.com/Fretadao/f_service'
|
21
21
|
spec.license = 'MIT'
|
22
|
+
spec.required_ruby_version = '>= 3.0.0'
|
22
23
|
|
23
24
|
spec.metadata['homepage_uri'] = spec.homepage
|
24
25
|
spec.metadata['source_code_uri'] = 'https://github.com/Fretadao/f_service'
|
@@ -33,6 +34,4 @@ Gem::Specification.new do |spec|
|
|
33
34
|
spec.bindir = 'exe'
|
34
35
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
35
36
|
spec.require_paths = ['lib']
|
36
|
-
|
37
|
-
spec.add_development_dependency 'bundler', '~> 2.0'
|
38
37
|
end
|
data/lib/f_service/base.rb
CHANGED
@@ -101,69 +101,70 @@ module FService
|
|
101
101
|
def success(data = nil)
|
102
102
|
FService.deprecate!(
|
103
103
|
name: "#{self.class}##{__method__}",
|
104
|
-
alternative: '#Success'
|
104
|
+
alternative: '#Success',
|
105
|
+
from: caller[0]
|
105
106
|
)
|
106
107
|
|
107
108
|
Result::Success.new(data)
|
108
109
|
end
|
109
110
|
|
110
111
|
# Returns a successful result.
|
111
|
-
# You can optionally specify a
|
112
|
+
# You can optionally specify a list of types and a value for your result.
|
112
113
|
# You'll probably want to return this inside {#run}.
|
113
114
|
#
|
114
115
|
#
|
115
116
|
# @example
|
116
117
|
# def run
|
117
118
|
# Success()
|
118
|
-
# # => #<Success @value=nil, @
|
119
|
+
# # => #<Success @value=nil, @types=[]>
|
119
120
|
#
|
120
121
|
# Success(:ok)
|
121
|
-
# # => #<Success @value=nil, @
|
122
|
+
# # => #<Success @value=nil, @types=[:ok]>
|
122
123
|
#
|
123
124
|
# Success(data: 10)
|
124
|
-
# # => #<Success @value=10, @
|
125
|
+
# # => #<Success @value=10, @types=[]>
|
125
126
|
#
|
126
127
|
# Success(:ok, data: 10)
|
127
|
-
# # => #<Success @value=10, @
|
128
|
+
# # => #<Success @value=10, @types=[:ok]>
|
128
129
|
# end
|
129
130
|
#
|
130
|
-
# @param
|
131
|
+
# @param types the Result types
|
131
132
|
# @param data the result value
|
132
133
|
# @return [Result::Success] a successful result
|
133
|
-
def Success(
|
134
|
-
Result::Success.new(data,
|
134
|
+
def Success(*types, data: nil)
|
135
|
+
Result::Success.new(data, types)
|
135
136
|
end
|
136
137
|
|
137
138
|
# Returns a failed result.
|
138
|
-
# You can optionally specify
|
139
|
+
# You can optionally specify types and a value for your result.
|
139
140
|
# You'll probably want to return this inside {#run}.
|
140
141
|
#
|
141
142
|
#
|
142
143
|
# @example
|
143
144
|
# def run
|
144
145
|
# Failure()
|
145
|
-
# # => #<Failure @error=nil, @
|
146
|
+
# # => #<Failure @error=nil, @types=[]>
|
146
147
|
#
|
147
148
|
# Failure(:not_a_number)
|
148
|
-
# # => #<Failure @error=nil, @
|
149
|
+
# # => #<Failure @error=nil, @types=[:not_a_number]>
|
149
150
|
#
|
150
151
|
# Failure(data: "10")
|
151
|
-
# # => #<Failure @error="10", @
|
152
|
+
# # => #<Failure @error="10", @types=[]>
|
152
153
|
#
|
153
154
|
# Failure(:not_a_number, data: "10")
|
154
|
-
# # => #<Failure @error="10", @
|
155
|
+
# # => #<Failure @error="10", @types=[:not_a_number]>
|
155
156
|
# end
|
156
157
|
#
|
157
|
-
# @param
|
158
|
+
# @param types the Result types
|
158
159
|
# @param data the result value
|
159
160
|
# @return [Result::Failure] a failed result
|
160
|
-
def Failure(
|
161
|
-
Result::Failure.new(data,
|
161
|
+
def Failure(*types, data: nil)
|
162
|
+
Result::Failure.new(data, types)
|
162
163
|
end
|
163
164
|
|
164
165
|
# Converts a boolean to a Result.
|
165
166
|
# Truthy values map to Success, and falsey values map to Failures.
|
166
|
-
# You can optionally provide a
|
167
|
+
# You can optionally provide a types for the result.
|
167
168
|
# The result value defaults as the evaluated value of the given block.
|
168
169
|
# If you want another value you can pass it through the `data:` argument.
|
169
170
|
#
|
@@ -171,34 +172,34 @@ module FService
|
|
171
172
|
# class CheckMathWorks < FService::Base
|
172
173
|
# def run
|
173
174
|
# Check(:math_works) { 1 < 2 }
|
174
|
-
# # => #<Success @value=true, @
|
175
|
+
# # => #<Success @value=true, @types=[:math_works]>
|
175
176
|
#
|
176
177
|
# Check(:math_works) { 1 > 2 }
|
177
|
-
# # => #<Failure @error=false, @
|
178
|
+
# # => #<Failure @error=false, @types=[:math_works]>
|
178
179
|
#
|
179
180
|
# Check(:math_works, data: 1 + 2) { 1 > 2 }
|
180
|
-
# # => #<Failure @
|
181
|
+
# # => #<Failure @types=:math_works, @error=3>
|
181
182
|
# end
|
182
183
|
#
|
183
184
|
# Check(:math_works, data: 1 + 2) { 1 < 2 }
|
184
|
-
# # => #<Success @
|
185
|
+
# # => #<Success @types=[:math_works], @value=3>
|
185
186
|
# end
|
186
187
|
# end
|
187
188
|
#
|
188
|
-
# @param
|
189
|
+
# @param types the Result types
|
189
190
|
# @return [Result::Success, Result::Failure] a Result from the boolean expression
|
190
|
-
def Check(
|
191
|
+
def Check(*types, data: nil)
|
191
192
|
res = yield
|
192
193
|
|
193
194
|
final_data = data || res
|
194
195
|
|
195
|
-
res ? Success(
|
196
|
+
res ? Success(*types, data: final_data) : Failure(*types, data: final_data)
|
196
197
|
end
|
197
198
|
|
198
199
|
# If the given block raises an exception, it wraps it in a Failure.
|
199
200
|
# Otherwise, maps the block value in a Success object.
|
200
201
|
# You can specify which exceptions to watch for.
|
201
|
-
# It's possible to provide a
|
202
|
+
# It's possible to provide a types for the result too.
|
202
203
|
#
|
203
204
|
# @example
|
204
205
|
# class IHateEvenNumbers < FService::Base
|
@@ -213,20 +214,20 @@ module FService
|
|
213
214
|
# end
|
214
215
|
#
|
215
216
|
# IHateEvenNumbers.call
|
216
|
-
# # => #<Success @value=9, @
|
217
|
+
# # => #<Success @value=9, @types=[:rand_int]>
|
217
218
|
#
|
218
219
|
# IHateEvenNumbers.call
|
219
|
-
# # => #<Failure @error=#<RuntimeError: Yuck! It's a 4>, @
|
220
|
+
# # => #<Failure @error=#<RuntimeError: Yuck! It's a 4>, @types=[:rand_int]>
|
220
221
|
#
|
221
|
-
# @param
|
222
|
+
# @param types the Result types
|
222
223
|
# @param catch the exception list to catch
|
223
224
|
# @return [Result::Success, Result::Failure] a result from the boolean expression
|
224
|
-
def Try(
|
225
|
+
def Try(*types, catch: StandardError)
|
225
226
|
res = yield
|
226
227
|
|
227
|
-
Success(
|
228
|
+
Success(*types, data: res)
|
228
229
|
rescue *catch => e
|
229
|
-
Failure(
|
230
|
+
Failure(*types, data: e)
|
230
231
|
end
|
231
232
|
|
232
233
|
# Returns a failed operation.
|
@@ -250,7 +251,8 @@ module FService
|
|
250
251
|
def failure(data = nil)
|
251
252
|
FService.deprecate!(
|
252
253
|
name: "#{self.class}##{__method__}",
|
253
|
-
alternative: '#Failure'
|
254
|
+
alternative: '#Failure',
|
255
|
+
from: caller[0]
|
254
256
|
)
|
255
257
|
|
256
258
|
Result::Failure.new(data)
|
@@ -283,7 +285,8 @@ module FService
|
|
283
285
|
def result(condition, data = nil)
|
284
286
|
FService.deprecate!(
|
285
287
|
name: "#{self.class}##{__method__}",
|
286
|
-
alternative: '#Check'
|
288
|
+
alternative: '#Check',
|
289
|
+
from: caller[0]
|
287
290
|
)
|
288
291
|
|
289
292
|
condition ? success(data) : failure(data)
|
@@ -7,24 +7,38 @@ module FService
|
|
7
7
|
#
|
8
8
|
# @abstract
|
9
9
|
class Base
|
10
|
-
|
10
|
+
attr_reader :types
|
11
|
+
|
12
|
+
%i[and_then successful? failed? value value! error].each do |method_name|
|
11
13
|
define_method(method_name) do |*_args|
|
12
14
|
raise NotImplementedError, "called #{method_name} on class Result::Base"
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
# You usually shouldn't call this directly. See {FService::Base#Failure} and {FService::Base#Success}.
|
19
|
+
def initialize(types = [])
|
20
|
+
@handled = false
|
21
|
+
@types = types
|
22
|
+
@matching_types = []
|
23
|
+
end
|
24
|
+
|
25
|
+
# Implements old attribute type. Its deprecated in favor of using types.
|
26
|
+
def type
|
27
|
+
FService.deprecate!(name: "#{self.class}##{__method__}", alternative: '#types', from: caller[0])
|
28
|
+
|
29
|
+
types.size == 1 ? types.first : Array(@matching_types).first
|
30
|
+
end
|
31
|
+
|
32
|
+
# This hook runs if the result is successful.
|
33
|
+
# Can receive one or more types to be checked before running the given block.
|
20
34
|
#
|
21
35
|
# @example
|
22
36
|
# class UsersController < BaseController
|
23
37
|
# def update
|
24
|
-
# User::Update.(user: user)
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
38
|
+
# User::Update.(user: user)
|
39
|
+
# .on_success(:type, :type2) { return json_success({ status: :ok }) } # run only if type matches
|
40
|
+
# .on_success { |value| return json_success(value) }
|
41
|
+
# .on_failure { |error| return json_error(error) } # this won't run
|
28
42
|
# end
|
29
43
|
#
|
30
44
|
# private
|
@@ -34,33 +48,13 @@ module FService
|
|
34
48
|
# end
|
35
49
|
# end
|
36
50
|
#
|
37
|
-
# @param success [#call] a lambda (or anything that responds to #call) to run on success
|
38
|
-
# @param failure [#call] a lambda (or anything that responds to #call) to run on failure
|
39
|
-
# @deprecated Use {#on_success} and/or {#on_failure} instead.
|
40
|
-
# @api public
|
41
|
-
def on(success:, failure:)
|
42
|
-
FService.deprecate!(
|
43
|
-
name: "#{self.class}##{__method__}",
|
44
|
-
alternative: '#on_success and/or #on_failure'
|
45
|
-
)
|
46
|
-
|
47
|
-
if successful?
|
48
|
-
success.call(value)
|
49
|
-
else
|
50
|
-
failure.call(error)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
# This hook runs if the result is successful.
|
55
|
-
# Can receive one or more types to be checked before running the given block.
|
56
|
-
#
|
57
51
|
# @example
|
58
52
|
# class UsersController < BaseController
|
59
53
|
# def update
|
60
54
|
# User::Update.(user: user)
|
61
55
|
# .on_success(:type, :type2) { return json_success({ status: :ok }) } # run only if type matches
|
62
|
-
# .on_success { |value| return json_success(value) }
|
63
|
-
# .on_failure { |error| return json_error(error) } # this won't run
|
56
|
+
# .on_success(unhandled: true) { |value| return json_success(value) }
|
57
|
+
# .on_failure(unhandled: true) { |error| return json_error(error) } # this won't run
|
64
58
|
# end
|
65
59
|
#
|
66
60
|
# private
|
@@ -74,8 +68,13 @@ module FService
|
|
74
68
|
# @yieldparam type type of the failure object
|
75
69
|
# @return [Success, Failure] the original Result object
|
76
70
|
# @api public
|
77
|
-
def on_success(*target_types)
|
78
|
-
|
71
|
+
def on_success(*target_types, unhandled: false)
|
72
|
+
if successful? && unhandled? && expected_type?(target_types, unhandled: unhandled)
|
73
|
+
match_types(target_types)
|
74
|
+
yield(*to_ary)
|
75
|
+
@handled = true
|
76
|
+
freeze
|
77
|
+
end
|
79
78
|
|
80
79
|
self
|
81
80
|
end
|
@@ -99,12 +98,33 @@ module FService
|
|
99
98
|
# end
|
100
99
|
# end
|
101
100
|
#
|
101
|
+
# @example
|
102
|
+
# class UsersController < BaseController
|
103
|
+
# def update
|
104
|
+
# User::Update.(user: user)
|
105
|
+
# .on_success(:unhandled: true) { |value| return json_success(value) } # this won't run
|
106
|
+
# .on_failure(:type, :type2) { |error| return json_error(error) } # runs only if type matches
|
107
|
+
# .on_failure(:unhandled: true) { |error| return json_error(error) }
|
108
|
+
# end
|
109
|
+
#
|
110
|
+
# private
|
111
|
+
#
|
112
|
+
# def user
|
113
|
+
# @user ||= User.find_by!(slug: params[:slug])
|
114
|
+
# end
|
115
|
+
# end
|
116
|
+
#
|
102
117
|
# @yieldparam value value of the failure object
|
103
118
|
# @yieldparam type type of the failure object
|
104
119
|
# @return [Success, Failure] the original Result object
|
105
120
|
# @api public
|
106
|
-
def on_failure(*target_types)
|
107
|
-
|
121
|
+
def on_failure(*target_types, unhandled: false)
|
122
|
+
if failed? && unhandled? && expected_type?(target_types, unhandled: unhandled)
|
123
|
+
match_types(target_types)
|
124
|
+
yield(*to_ary)
|
125
|
+
@handled = true
|
126
|
+
freeze
|
127
|
+
end
|
108
128
|
|
109
129
|
self
|
110
130
|
end
|
@@ -115,13 +135,25 @@ module FService
|
|
115
135
|
def to_ary
|
116
136
|
data = successful? ? value : error
|
117
137
|
|
118
|
-
[data,
|
138
|
+
[data, @matching_types.first]
|
119
139
|
end
|
120
140
|
|
121
141
|
private
|
122
142
|
|
123
|
-
def
|
124
|
-
|
143
|
+
def handled?
|
144
|
+
@handled
|
145
|
+
end
|
146
|
+
|
147
|
+
def unhandled?
|
148
|
+
!handled?
|
149
|
+
end
|
150
|
+
|
151
|
+
def expected_type?(target_types, unhandled:)
|
152
|
+
target_types.empty? || unhandled || target_types.any? { |target_type| types.include?(target_type) }
|
153
|
+
end
|
154
|
+
|
155
|
+
def match_types(target_types)
|
156
|
+
@matching_types = target_types.empty? ? types : target_types & types
|
125
157
|
end
|
126
158
|
end
|
127
159
|
end
|
@@ -10,20 +10,19 @@ module FService
|
|
10
10
|
#
|
11
11
|
# @!attribute [r] error
|
12
12
|
# @return [Object] the provided error for the result
|
13
|
-
# @!attribute [r]
|
14
|
-
# @return [Object] the provided
|
13
|
+
# @!attribute [r] types
|
14
|
+
# @return [Object] the provided types for the result. Defaults to nil.
|
15
15
|
# @api public
|
16
16
|
class Failure < Result::Base
|
17
|
-
attr_reader :error
|
17
|
+
attr_reader :error
|
18
18
|
|
19
19
|
# Creates a failed operation.
|
20
20
|
# You usually shouldn't call this directly. See {FService::Base#Failure}.
|
21
21
|
#
|
22
22
|
# @param error [Object] failure value.
|
23
|
-
def initialize(error,
|
23
|
+
def initialize(error, types = [])
|
24
|
+
super(types)
|
24
25
|
@error = error
|
25
|
-
@type = type
|
26
|
-
freeze
|
27
26
|
end
|
28
27
|
|
29
28
|
# Returns false.
|
@@ -110,7 +109,12 @@ module FService
|
|
110
109
|
def and_then
|
111
110
|
self
|
112
111
|
end
|
113
|
-
|
112
|
+
|
113
|
+
# See #and_then
|
114
|
+
def then
|
115
|
+
FService.deprecate!(name: "#{self.class}##{__method__}", alternative: '#and_then', from: caller[0])
|
116
|
+
and_then
|
117
|
+
end
|
114
118
|
|
115
119
|
# Outputs a string representation of the object
|
116
120
|
#
|
@@ -9,20 +9,19 @@ module FService
|
|
9
9
|
#
|
10
10
|
# @!attribute [r] value
|
11
11
|
# @return [Object] the provided value for the result
|
12
|
-
# @!attribute [r]
|
13
|
-
# @return [Object] the provided
|
12
|
+
# @!attribute [r] types
|
13
|
+
# @return [Object] the provided types for the result. Defaults to nil.
|
14
14
|
# @api public
|
15
15
|
class Success < Result::Base
|
16
|
-
attr_reader :value
|
16
|
+
attr_reader :value
|
17
17
|
|
18
18
|
# Creates a successful operation.
|
19
19
|
# You usually shouldn't call this directly. See {FService::Base#Success}.
|
20
20
|
#
|
21
21
|
# @param value [Object] success value.
|
22
|
-
def initialize(value,
|
22
|
+
def initialize(value, types = [])
|
23
|
+
super(types)
|
23
24
|
@value = value
|
24
|
-
@type = type
|
25
|
-
freeze
|
26
25
|
end
|
27
26
|
|
28
27
|
# Returns true.
|
@@ -79,11 +78,17 @@ module FService
|
|
79
78
|
# end
|
80
79
|
#
|
81
80
|
# @yieldparam value pass {#value} to a block
|
82
|
-
# @yieldparam
|
81
|
+
# @yieldparam types pass {#types} to a block
|
83
82
|
def and_then
|
84
83
|
yield(*to_ary)
|
85
84
|
end
|
86
|
-
|
85
|
+
|
86
|
+
# See #and_then
|
87
|
+
def then(&block)
|
88
|
+
FService.deprecate!(name: "#{self.class}##{__method__}", alternative: '#and_then', from: caller[0])
|
89
|
+
|
90
|
+
and_then(&block)
|
91
|
+
end
|
87
92
|
|
88
93
|
# Returns itself to the given block.
|
89
94
|
# Use this to chain multiple actions or service calls (only valid when they return a Result).
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Methods to mock a FService result from a service call.
|
4
|
+
module FServiceResultHelpers
|
5
|
+
# Create an Fservice result Success or Failure.
|
6
|
+
def f_service_result(result, value = nil, types = [])
|
7
|
+
if result == :success
|
8
|
+
FService::Result::Success.new(value, Array(types))
|
9
|
+
else
|
10
|
+
FService::Result::Failure.new(value, Array(types))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Mock a Fservice service call returning a result.
|
15
|
+
def mock_service(service, result: :success, value: nil, type: :not_passed, types: [])
|
16
|
+
result_types = Array(types)
|
17
|
+
|
18
|
+
if type != :not_passed
|
19
|
+
alternative = "mock_service(..., types: [#{type.inspect}])"
|
20
|
+
name = 'mock_service'
|
21
|
+
FService.deprecate_argument_name(name: name, argument_name: :type, alternative: alternative, from: caller[0])
|
22
|
+
result_types = Array(type)
|
23
|
+
end
|
24
|
+
|
25
|
+
service_result = f_service_result(result, value, result_types)
|
26
|
+
allow(service).to receive(:call).and_return(service_result)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
RSpec.configure do |config|
|
31
|
+
config.include FServiceResultHelpers
|
32
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rspec/expectations'
|
4
|
+
|
5
|
+
RSpec::Matchers.define :have_failed_with do |*expected_types|
|
6
|
+
match do |actual|
|
7
|
+
matched = actual.is_a?(FService::Result::Failure) && actual.types == expected_types
|
8
|
+
|
9
|
+
matched &&= values_match?(@expected_error, actual.error) if defined?(@expected_error)
|
10
|
+
|
11
|
+
matched
|
12
|
+
end
|
13
|
+
|
14
|
+
chain :and_error do |expected_error|
|
15
|
+
@expected_error = expected_error
|
16
|
+
end
|
17
|
+
|
18
|
+
failure_message do |actual|
|
19
|
+
if actual.is_a?(FService::Result::Failure)
|
20
|
+
message = "expected failure's types '#{actual.types.inspect}' to be equal '#{expected_types.inspect}'"
|
21
|
+
if defined?(@expected_error)
|
22
|
+
has_description = @expected_error.respond_to?(:description)
|
23
|
+
message += " and error '#{actual.error.inspect}' to be "
|
24
|
+
message += has_description ? @expected_error.description : "equal '#{@expected_error.inspect}'"
|
25
|
+
end
|
26
|
+
|
27
|
+
message
|
28
|
+
else
|
29
|
+
"result '#{actual.inspect}' is not a Failure object"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Matchers.define :have_succeed_with do |*expected_types|
|
35
|
+
match do |actual|
|
36
|
+
matched = actual.is_a?(FService::Result::Success) && actual.types == expected_types
|
37
|
+
|
38
|
+
matched &&= values_match?(@expected_value, actual.value) if defined?(@expected_value)
|
39
|
+
|
40
|
+
matched
|
41
|
+
end
|
42
|
+
|
43
|
+
chain :and_value do |expected_value|
|
44
|
+
@expected_value = expected_value
|
45
|
+
end
|
46
|
+
|
47
|
+
failure_message do |actual|
|
48
|
+
if actual.is_a?(FService::Result::Success)
|
49
|
+
message = "expected success's types '#{actual.types.inspect}' to be equal '#{expected_types.inspect}'"
|
50
|
+
if defined?(@expected_value)
|
51
|
+
has_description = @expected_value.respond_to?(:description)
|
52
|
+
message += " and value '#{actual.value.inspect}' to be "
|
53
|
+
message += has_description ? @expected_value.description : "equal '#{@expected_value.inspect}'"
|
54
|
+
end
|
55
|
+
|
56
|
+
message
|
57
|
+
else
|
58
|
+
"result '#{actual.inspect}' is not a Success object"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/f_service/version.rb
CHANGED
data/lib/f_service.rb
CHANGED
@@ -9,9 +9,24 @@ module FService
|
|
9
9
|
# Marks a method as deprecated
|
10
10
|
#
|
11
11
|
# @api private
|
12
|
-
def self.deprecate!(name:, alternative:)
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
def self.deprecate!(name:, alternative:, from: nil)
|
13
|
+
warn_message = ["\n[DEPRECATED] #{name} is deprecated; "]
|
14
|
+
warn_message << ["called from #{from}; "] unless from.nil?
|
15
|
+
warn_message << "use #{alternative} instead. "
|
16
|
+
warn_message << 'It will be removed on the next release.'
|
17
|
+
|
18
|
+
warn warn_message.join("\n")
|
19
|
+
end
|
20
|
+
|
21
|
+
# Marks an argument as deprecated
|
22
|
+
#
|
23
|
+
# @api private
|
24
|
+
def self.deprecate_argument_name(name:, argument_name:, alternative:, from: nil)
|
25
|
+
warn_message = ["\n[DEPRECATED] #{name} passing #{argument_name.inspect} is deprecated; "]
|
26
|
+
warn_message << ["called from #{from}; "] unless from.nil?
|
27
|
+
warn_message << "use #{alternative} instead. "
|
28
|
+
warn_message << 'It will be removed on the next release.'
|
29
|
+
|
30
|
+
warn warn_message.join("\n")
|
16
31
|
end
|
17
32
|
end
|
metadata
CHANGED
@@ -1,39 +1,26 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: f_service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Fretadao Tech Team
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '2.0'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '2.0'
|
11
|
+
date: 2024-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
27
13
|
description: |2
|
28
14
|
FService is a small gem that provides a base class for your services (aka operations).
|
29
15
|
The goal is to make services simpler, safer and more composable.
|
30
16
|
It uses the Result monad for handling operations.
|
31
17
|
email:
|
32
|
-
-
|
18
|
+
- tech@fretadao.com.br
|
33
19
|
executables: []
|
34
20
|
extensions: []
|
35
21
|
extra_rdoc_files: []
|
36
22
|
files:
|
23
|
+
- ".github/.dependabot.yml"
|
37
24
|
- ".github/workflows/tests-and-linter.yml"
|
38
25
|
- ".gitignore"
|
39
26
|
- ".rubocop.yml"
|
@@ -52,6 +39,12 @@ files:
|
|
52
39
|
- lib/f_service/result/base.rb
|
53
40
|
- lib/f_service/result/failure.rb
|
54
41
|
- lib/f_service/result/success.rb
|
42
|
+
- lib/f_service/rspec.rb
|
43
|
+
- lib/f_service/rspec/support.rb
|
44
|
+
- lib/f_service/rspec/support/helpers.rb
|
45
|
+
- lib/f_service/rspec/support/helpers/result.rb
|
46
|
+
- lib/f_service/rspec/support/matchers.rb
|
47
|
+
- lib/f_service/rspec/support/matchers/result.rb
|
55
48
|
- lib/f_service/version.rb
|
56
49
|
- logo.png
|
57
50
|
homepage: https://github.com/Fretadao/f_service
|
@@ -70,14 +63,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
63
|
requirements:
|
71
64
|
- - ">="
|
72
65
|
- !ruby/object:Gem::Version
|
73
|
-
version:
|
66
|
+
version: 3.0.0
|
74
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
68
|
requirements:
|
76
69
|
- - ">="
|
77
70
|
- !ruby/object:Gem::Version
|
78
71
|
version: '0'
|
79
72
|
requirements: []
|
80
|
-
rubygems_version: 3.
|
73
|
+
rubygems_version: 3.5.11
|
81
74
|
signing_key:
|
82
75
|
specification_version: 4
|
83
76
|
summary: A small, monad-based service class
|