service_pattern 1.0.0 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -2
- data/lib/service_pattern/response.rb +17 -0
- data/lib/service_pattern/service.rb +16 -14
- data/lib/service_pattern/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6127af0c6cf5dff009118b626338c47e85cd92f920a3721b90ff17177b6d2d6f
|
4
|
+
data.tar.gz: f9168398a91ef556e6aeff33f17e72b893e0d0c183d5923874b735278752ed61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55e3cb0164fc764be393f50e5ced27f2ccf698a2459d8416148692fff790317e65af55099150550099ea8a07ccf2e8badab5980d464a832ae7c40bc647f2ec63
|
7
|
+
data.tar.gz: 1f027e60f2e958df5261486e011558b0d9498765832d3f45dd7caebec6d6c1656f5b6c84da8b664ac4612aa2fa0f5c99c69b69da7b3bd72f3629c1d7e20194e3
|
data/README.md
CHANGED
@@ -35,7 +35,7 @@ response = Users::ActivatorService.()
|
|
35
35
|
if response.success?
|
36
36
|
puts "Result: #{response.result}"
|
37
37
|
else
|
38
|
-
puts "Errors: #{response.
|
38
|
+
puts "Errors: #{response.error_messages.join(". ")}"
|
39
39
|
end
|
40
40
|
```
|
41
41
|
|
@@ -46,10 +46,17 @@ response = Users::ActivatorService.execute()
|
|
46
46
|
if response.success?
|
47
47
|
puts "Result: #{response.result}"
|
48
48
|
else
|
49
|
-
puts "Errors: #{response.
|
49
|
+
puts "Errors: #{response.error_messages.join(". ")}"
|
50
|
+
puts "Custom error? #{response.error_type?(:custom_error) ? "Yes" : "No"}"
|
51
|
+
puts "Only custom error? #{response.only_error_type?(:custom_error) ? "Yes" : "No"}"
|
50
52
|
end
|
51
53
|
```
|
52
54
|
|
55
|
+
Raise a normal service error unless error is of a specific type.
|
56
|
+
```ruby
|
57
|
+
response.raise_error! unless response.only_error_type?(:custom_error)
|
58
|
+
```
|
59
|
+
|
53
60
|
Or raise an error if it fails and return the result directly:
|
54
61
|
```ruby
|
55
62
|
result = Users::ActivatorService.execute!
|
@@ -80,5 +87,10 @@ Or with multiple errors:
|
|
80
87
|
fail! ["Hello world", "Hello again"]
|
81
88
|
```
|
82
89
|
|
90
|
+
Or with error types:
|
91
|
+
```ruby
|
92
|
+
fail! "Hello world", type: :message
|
93
|
+
```
|
94
|
+
|
83
95
|
## License
|
84
96
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -15,6 +15,23 @@ class ServicePattern::Response
|
|
15
15
|
@error_types ||= @errors.map(&:type).reject(&:blank?)
|
16
16
|
end
|
17
17
|
|
18
|
+
def error_type?(type)
|
19
|
+
error_types.include?(type)
|
20
|
+
end
|
21
|
+
|
22
|
+
def only_error_type?(type)
|
23
|
+
error_types.length == 1 && error_type?(type)
|
24
|
+
end
|
25
|
+
|
26
|
+
def raise_error!
|
27
|
+
return if errors.empty?
|
28
|
+
|
29
|
+
error = ServicePattern::FailedError.new(error_messages.join(". "))
|
30
|
+
error.errors = errors
|
31
|
+
|
32
|
+
raise error
|
33
|
+
end
|
34
|
+
|
18
35
|
def success?
|
19
36
|
@success
|
20
37
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class ServicePattern::Service
|
2
2
|
# The same as execute but doesn't catch FailedError so they are passed on to the parent service call
|
3
|
-
def self.chain(*args, &blk)
|
4
|
-
service = new(*args, &blk)
|
3
|
+
def self.chain(*args, **opts, &blk)
|
4
|
+
service = new(*args, **opts, &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?
|
@@ -9,22 +9,16 @@ class ServicePattern::Service
|
|
9
9
|
service.perform
|
10
10
|
end
|
11
11
|
|
12
|
-
def self.call(*args, &blk)
|
13
|
-
execute(*args, &blk)
|
12
|
+
def self.call(*args, **opts, &blk)
|
13
|
+
execute(*args, **opts, &blk)
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.execute(*args, &blk)
|
17
|
-
|
18
|
-
service.execute
|
16
|
+
def self.execute(*args, **opts, &blk)
|
17
|
+
new(*args, **opts, &blk).execute
|
19
18
|
end
|
20
19
|
|
21
|
-
def self.execute!(*args, &blk)
|
22
|
-
|
23
|
-
can_execute_response = service.can_execute?
|
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
|
20
|
+
def self.execute!(*args, **opts, &blk)
|
21
|
+
new(*args, **opts, &blk).execute!
|
28
22
|
end
|
29
23
|
|
30
24
|
def self.convert_errors(errors)
|
@@ -58,6 +52,14 @@ class ServicePattern::Service
|
|
58
52
|
ServicePattern::Response.new(errors: e.errors)
|
59
53
|
end
|
60
54
|
|
55
|
+
def execute!
|
56
|
+
can_execute_response = can_execute?
|
57
|
+
ServicePattern::Service.fail!(can_execute_response.errors) unless can_execute_response.success?
|
58
|
+
response = perform
|
59
|
+
ServicePattern::Service.fail!(response.errors) unless response.success?
|
60
|
+
response.result
|
61
|
+
end
|
62
|
+
|
61
63
|
def perform(*_args)
|
62
64
|
raise NoMethodError, "You should implement the `perform` method on your service"
|
63
65
|
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: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kaspernj
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ServicePattern for Ruby on Rails.
|
14
14
|
email:
|
@@ -39,14 +39,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
39
|
requirements:
|
40
40
|
- - ">="
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: 2.
|
42
|
+
version: 2.7.0
|
43
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
requirements: []
|
49
|
-
rubygems_version: 3.
|
49
|
+
rubygems_version: 3.2.32
|
50
50
|
signing_key:
|
51
51
|
specification_version: 4
|
52
52
|
summary: ServicePattern for Ruby on Rails.
|