service_pattern 0.0.8 → 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/service.rb +22 -17
- 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: 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
|
@@ -6,7 +6,7 @@ class ServicePattern::Service
|
|
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)
|
@@ -14,23 +14,11 @@ class ServicePattern::Service
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.execute(*args, &blk)
|
17
|
-
|
18
|
-
|
19
|
-
can_execute_response = service.can_execute?
|
20
|
-
return can_execute_response unless can_execute_response.success?
|
21
|
-
|
22
|
-
service.execute
|
23
|
-
rescue ServicePattern::FailedError => e
|
24
|
-
ServicePattern::Response.new(errors: e.errors)
|
17
|
+
new(*args, &blk).execute
|
25
18
|
end
|
26
19
|
|
27
20
|
def self.execute!(*args, &blk)
|
28
|
-
|
29
|
-
can_execute_response = service.can_execute?
|
30
|
-
ServicePattern::Service.fail!(can_execute_response.errors) unless can_execute_response.success?
|
31
|
-
response = service.execute
|
32
|
-
ServicePattern::Service.fail!(response.errors) unless response.success?
|
33
|
-
response.result
|
21
|
+
new(*args, &blk).execute!
|
34
22
|
end
|
35
23
|
|
36
24
|
def self.convert_errors(errors)
|
@@ -55,8 +43,25 @@ class ServicePattern::Service
|
|
55
43
|
succeed!
|
56
44
|
end
|
57
45
|
|
58
|
-
def execute
|
59
|
-
|
46
|
+
def execute
|
47
|
+
can_execute_response = can_execute?
|
48
|
+
return can_execute_response unless can_execute_response.success?
|
49
|
+
|
50
|
+
perform
|
51
|
+
rescue ServicePattern::FailedError => e
|
52
|
+
ServicePattern::Response.new(errors: e.errors)
|
53
|
+
end
|
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
|
+
|
63
|
+
def perform(*_args)
|
64
|
+
raise NoMethodError, "You should implement the `perform` method on your service"
|
60
65
|
end
|
61
66
|
|
62
67
|
def fail!(errors, type: nil)
|
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:
|
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:
|
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,14 +39,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
39
|
requirements:
|
40
40
|
- - ">="
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version:
|
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.1.6
|
50
50
|
signing_key:
|
51
51
|
specification_version: 4
|
52
52
|
summary: ServicePattern for Ruby on Rails.
|