service_pattern 0.0.4 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +32 -4
- data/lib/service_pattern.rb +1 -0
- data/lib/service_pattern/fail_error.rb +8 -0
- data/lib/service_pattern/failed_error.rb +11 -1
- data/lib/service_pattern/response.rb +11 -12
- data/lib/service_pattern/service.rb +46 -13
- data/lib/service_pattern/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cdb56345fc9435ba6661951aa8d3cc5176022c2767881b340b6551c94c8e4caa
|
4
|
+
data.tar.gz: f9fc3f3ae0262dfa6c0ff58505cce750070074bdf9cb8d52ed826695ec3eb46e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5466648e3d09f9add63cd3205a94f4f27e9d732a322e84eba8982f16d8d448d745669fcdbd38ddb04a5ac29663491129ef7ef315589b37c5a5e43fe118f94563
|
7
|
+
data.tar.gz: b1dfe83d17699a4eb998a7a85614e735979443c2baedd27d51d65549a2896e4377b65c150c6b0a55889d194d6d25c7212fe6ea90a8db31ab62f9a23393d31b4d
|
data/README.md
CHANGED
@@ -23,7 +23,7 @@ Create your first service in "app/services/users/activator_service":
|
|
23
23
|
class Users::ActivatorService < ApplicationService
|
24
24
|
def execute
|
25
25
|
User.all.find_each(&:activate!)
|
26
|
-
|
26
|
+
succeed!
|
27
27
|
end
|
28
28
|
end
|
29
29
|
```
|
@@ -33,17 +33,35 @@ Then call it like this:
|
|
33
33
|
response = Users::ActivatorService.()
|
34
34
|
|
35
35
|
if response.success?
|
36
|
-
puts "
|
36
|
+
puts "Result: #{response.result}"
|
37
37
|
else
|
38
|
-
puts "Errors: #{
|
38
|
+
puts "Errors: #{response.errors.join(". ")}"
|
39
39
|
end
|
40
40
|
```
|
41
41
|
|
42
|
+
Or like this:
|
43
|
+
```ruby
|
44
|
+
response = Users::ActivatorService.execute()
|
45
|
+
|
46
|
+
if response.success?
|
47
|
+
puts "Result: #{response.result}"
|
48
|
+
else
|
49
|
+
puts "Errors: #{response.errors.join(". ")}"
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
Or raise an error if it fails and return the result directly:
|
54
|
+
```ruby
|
55
|
+
result = Users::ActivatorService.execute!
|
56
|
+
|
57
|
+
puts "Result: #{result}"
|
58
|
+
```
|
59
|
+
|
42
60
|
### Returning results
|
43
61
|
|
44
62
|
You can also return a result, which will automatically make the response successfull:
|
45
63
|
```ruby
|
46
|
-
|
64
|
+
succeed!(message: "Hello world")
|
47
65
|
```
|
48
66
|
|
49
67
|
You can then retrieve it like this:
|
@@ -52,5 +70,15 @@ response = Users::ActivatorService.()
|
|
52
70
|
puts "Result: #{response.result}"
|
53
71
|
```
|
54
72
|
|
73
|
+
You can fail a service like this
|
74
|
+
```ruby
|
75
|
+
fail! "Hello world"
|
76
|
+
```
|
77
|
+
|
78
|
+
Or with multiple errors:
|
79
|
+
```ruby
|
80
|
+
fail! ["Hello world", "Hello again"]
|
81
|
+
```
|
82
|
+
|
55
83
|
## License
|
56
84
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/service_pattern.rb
CHANGED
@@ -1 +1,11 @@
|
|
1
|
-
class ServicePattern::FailedError < RuntimeError
|
1
|
+
class ServicePattern::FailedError < RuntimeError
|
2
|
+
attr_accessor :errors
|
3
|
+
|
4
|
+
def error_messages
|
5
|
+
@error_messages ||= @errors.map(&:message)
|
6
|
+
end
|
7
|
+
|
8
|
+
def error_types
|
9
|
+
@error_types ||= @errors.map(&:type)
|
10
|
+
end
|
11
|
+
end
|
@@ -1,19 +1,18 @@
|
|
1
1
|
class ServicePattern::Response
|
2
2
|
attr_reader :errors, :result
|
3
3
|
|
4
|
-
def initialize(
|
5
|
-
@errors =
|
6
|
-
@result =
|
4
|
+
def initialize(errors: [], result: nil)
|
5
|
+
@errors = ServicePattern::Service.convert_errors(errors)
|
6
|
+
@result = result
|
7
|
+
@success = !errors || errors.empty?
|
8
|
+
end
|
9
|
+
|
10
|
+
def error_messages
|
11
|
+
@error_messages ||= @errors.map(&:message)
|
12
|
+
end
|
7
13
|
|
8
|
-
|
9
|
-
|
10
|
-
elsif args.key?(:errors) && @errors.any?
|
11
|
-
@success = false
|
12
|
-
elsif @result
|
13
|
-
@success = true
|
14
|
-
else
|
15
|
-
raise "Couldn't figure out if it was a success"
|
16
|
-
end
|
14
|
+
def error_types
|
15
|
+
@error_types ||= @errors.map(&:type).reject(&:blank?)
|
17
16
|
end
|
18
17
|
|
19
18
|
def success?
|
@@ -4,9 +4,9 @@ class ServicePattern::Service
|
|
4
4
|
service = new(*args, &blk)
|
5
5
|
|
6
6
|
can_execute_response = service.can_execute?
|
7
|
-
|
7
|
+
ServicePattern::Service.fail!(can_execute_response.errors) unless can_execute_response.success?
|
8
8
|
|
9
|
-
service.
|
9
|
+
service.perform
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.call(*args, &blk)
|
@@ -15,27 +15,60 @@ class ServicePattern::Service
|
|
15
15
|
|
16
16
|
def self.execute(*args, &blk)
|
17
17
|
service = new(*args, &blk)
|
18
|
+
service.execute
|
19
|
+
end
|
18
20
|
|
21
|
+
def self.execute!(*args, &blk)
|
22
|
+
service = new(*args, &blk)
|
19
23
|
can_execute_response = service.can_execute?
|
20
|
-
|
24
|
+
ServicePattern::Service.fail!(can_execute_response.errors) unless can_execute_response.success?
|
25
|
+
response = service.perform
|
26
|
+
ServicePattern::Service.fail!(response.errors) unless response.success?
|
27
|
+
response.result
|
28
|
+
end
|
21
29
|
|
22
|
-
|
23
|
-
|
24
|
-
|
30
|
+
def self.convert_errors(errors)
|
31
|
+
errors = [errors] unless errors.is_a?(Array)
|
32
|
+
errors.map do |error|
|
33
|
+
error = ServicePattern::FailError.new(message: error) unless error.is_a?(ServicePattern::FailError)
|
34
|
+
error
|
35
|
+
end
|
25
36
|
end
|
26
37
|
|
27
|
-
def self.
|
28
|
-
|
29
|
-
|
38
|
+
def self.fail!(errors)
|
39
|
+
errors = convert_errors(errors)
|
40
|
+
error_messages = errors.map(&:message)
|
30
41
|
|
31
|
-
|
42
|
+
error = ServicePattern::FailedError.new(error_messages.join(". "))
|
43
|
+
error.errors = errors
|
44
|
+
|
45
|
+
raise error
|
32
46
|
end
|
33
47
|
|
34
48
|
def can_execute?
|
35
|
-
|
49
|
+
succeed!
|
50
|
+
end
|
51
|
+
|
52
|
+
def execute
|
53
|
+
can_execute_response = can_execute?
|
54
|
+
return can_execute_response unless can_execute_response.success?
|
55
|
+
|
56
|
+
perform
|
57
|
+
rescue ServicePattern::FailedError => e
|
58
|
+
ServicePattern::Response.new(errors: e.errors)
|
59
|
+
end
|
60
|
+
|
61
|
+
def perform(*_args)
|
62
|
+
raise NoMethodError, "You should implement the `perform` method on your service"
|
63
|
+
end
|
64
|
+
|
65
|
+
def fail!(errors, type: nil)
|
66
|
+
errors = [ServicePattern::FailError.new(message: errors, type: type)] if type
|
67
|
+
|
68
|
+
ServicePattern::Service.fail!(errors)
|
36
69
|
end
|
37
70
|
|
38
|
-
def
|
39
|
-
|
71
|
+
def succeed!(result = nil)
|
72
|
+
ServicePattern::Response.new(result: result)
|
40
73
|
end
|
41
74
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: service_pattern
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kaspernj
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ServicePattern for Ruby on Rails.
|
14
14
|
email:
|
@@ -21,6 +21,7 @@ files:
|
|
21
21
|
- README.md
|
22
22
|
- Rakefile
|
23
23
|
- lib/service_pattern.rb
|
24
|
+
- lib/service_pattern/fail_error.rb
|
24
25
|
- lib/service_pattern/failed_error.rb
|
25
26
|
- lib/service_pattern/response.rb
|
26
27
|
- lib/service_pattern/service.rb
|
@@ -38,7 +39,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
39
|
requirements:
|
39
40
|
- - ">="
|
40
41
|
- !ruby/object:Gem::Version
|
41
|
-
version:
|
42
|
+
version: 2.5.0
|
42
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
44
|
requirements:
|
44
45
|
- - ">="
|