service_pattern 0.0.4 → 0.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 +32 -4
- data/lib/service_pattern/failed_error.rb +3 -1
- data/lib/service_pattern/response.rb +4 -13
- data/lib/service_pattern/service.rb +20 -6
- data/lib/service_pattern/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 278a1fa25b8d796da5bbd34a1b9f25f4de20bc11e4567290284c04706955e67f
|
4
|
+
data.tar.gz: 839b2968eeecfcce9319b4730eb6b9f58a0fca4529c8a56c063a588af9d35812
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8b6f769bb27497d09a37956659e793ebfd1a57fdfa175070fd0ae731ac60e2738c213c0d3a979cd8c4448fe533dfe7f3aaaea506fe0918e156e7f8a6e29f957
|
7
|
+
data.tar.gz: c3e7c12b8d3e5bfce1f963e58dd26e0a540b0594deb2bc94fb5b1c46cb196e2424e2f279601f700b30b856bd1022eab818171d20ab9008e64b6cb52d7af51d12
|
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).
|
@@ -1,19 +1,10 @@
|
|
1
1
|
class ServicePattern::Response
|
2
2
|
attr_reader :errors, :result
|
3
3
|
|
4
|
-
def initialize(
|
5
|
-
@errors =
|
6
|
-
@result =
|
7
|
-
|
8
|
-
if args.key?(:success)
|
9
|
-
@success = args.fetch(:success)
|
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
|
4
|
+
def initialize(errors: [], result: nil)
|
5
|
+
@errors = errors
|
6
|
+
@result = result
|
7
|
+
@success = !errors || errors.empty?
|
17
8
|
end
|
18
9
|
|
19
10
|
def success?
|
@@ -4,7 +4,7 @@ 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
9
|
service.execute
|
10
10
|
end
|
@@ -21,21 +21,35 @@ class ServicePattern::Service
|
|
21
21
|
|
22
22
|
service.execute
|
23
23
|
rescue ServicePattern::FailedError => e
|
24
|
-
ServicePattern::Response.new(errors:
|
24
|
+
ServicePattern::Response.new(errors: e.errors)
|
25
25
|
end
|
26
26
|
|
27
27
|
def self.execute!(*args, &blk)
|
28
28
|
response = execute(*args, &blk)
|
29
|
-
|
30
|
-
|
29
|
+
ServicePattern::Service.fail!(response.errors) unless response.success?
|
31
30
|
response.result
|
32
31
|
end
|
33
32
|
|
33
|
+
def self.fail!(errors)
|
34
|
+
errors = [errors] unless errors.is_a?(Array)
|
35
|
+
error = ServicePattern::FailedError.new(errors.join(". "))
|
36
|
+
error.errors = errors
|
37
|
+
raise error
|
38
|
+
end
|
39
|
+
|
34
40
|
def can_execute?
|
35
|
-
|
41
|
+
succeed!
|
36
42
|
end
|
37
43
|
|
38
44
|
def execute(*_args)
|
39
|
-
raise NoMethodError, "You should implement the `execute
|
45
|
+
raise NoMethodError, "You should implement the `execute` method on your service"
|
46
|
+
end
|
47
|
+
|
48
|
+
def fail!(errors)
|
49
|
+
ServicePattern::Service.fail!(errors)
|
50
|
+
end
|
51
|
+
|
52
|
+
def succeed!(result = nil)
|
53
|
+
ServicePattern::Response.new(result: result)
|
40
54
|
end
|
41
55
|
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: 0.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: 2019-
|
11
|
+
date: 2019-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ServicePattern for Ruby on Rails.
|
14
14
|
email:
|