service_pattern 1.0.2 → 1.0.4
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/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3974115d7989020e2c18159642a70302af8a53ebe75eae03f68461a6a5bf3b4
|
4
|
+
data.tar.gz: 3bdfde19ad64208f82c459c2337dcefeb6820dbf9e4a703b66f7269881f31e08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0143b8fa5a7f978c9834f87b01f8bc646c329fff174ebd5e3777564f2e414477582c6a4fa87fafb9e3b45ab51a38212d3f2d931dbca1ac38c3ee920d8c7ed5b5
|
7
|
+
data.tar.gz: 3005e2fe4284282b50db8a56be54450bbfe90aecbfe765f541a41e6b91705e26a71afec1b2043130f8aee00d46f7643a7439b28f884175459fbba328102a8155
|
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
|
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.4
|
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-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ServicePattern for Ruby on Rails.
|
14
14
|
email:
|
@@ -39,7 +39,7 @@ 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
|
- - ">="
|