service_pattern 0.0.3 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +45 -4
- data/lib/service_pattern.rb +2 -1
- data/lib/service_pattern/fail_error.rb +8 -0
- data/lib/service_pattern/failed_error.rb +11 -0
- data/lib/service_pattern/response.rb +11 -12
- data/lib/service_pattern/service.rb +59 -17
- data/lib/service_pattern/version.rb +1 -1
- metadata +5 -5
- data/lib/service_pattern/cant_execute_error.rb +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8ba006abbb363e6e905b04bc28d034d3ce3500a1972569796b3a4535d399f2a5
|
4
|
+
data.tar.gz: d6843c0cba4fcfe46ea34dc4b7e650a9356566a4e4c6ce2703234a3f44b88ca9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f0069948ebd6189ab2915d74b5a5ef299a61628082b58f8913ec5224746322d4f354542b4ece26d8672d58b38668651bc95b1ab660b2e4cf55bbbb0d2130393
|
7
|
+
data.tar.gz: 1c5dc20138d2d26a3e3b2e93d292861fd981c4fe7afc2a984dc09e48fe46dfd51ac71bd7e9030507a5d4998ff93a9c03c897173a96ef594f2aa410fb524a6b64
|
data/README.md
CHANGED
@@ -21,9 +21,9 @@ end
|
|
21
21
|
Create your first service in "app/services/users/activator_service":
|
22
22
|
```ruby
|
23
23
|
class Users::ActivatorService < ApplicationService
|
24
|
-
def execute
|
24
|
+
def execute
|
25
25
|
User.all.find_each(&:activate!)
|
26
|
-
|
26
|
+
succeed!
|
27
27
|
end
|
28
28
|
end
|
29
29
|
```
|
@@ -33,11 +33,52 @@ 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
|
+
|
60
|
+
### Returning results
|
61
|
+
|
62
|
+
You can also return a result, which will automatically make the response successfull:
|
63
|
+
```ruby
|
64
|
+
succeed!(message: "Hello world")
|
65
|
+
```
|
66
|
+
|
67
|
+
You can then retrieve it like this:
|
68
|
+
```ruby
|
69
|
+
response = Users::ActivatorService.()
|
70
|
+
puts "Result: #{response.result}"
|
71
|
+
```
|
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
|
+
|
42
83
|
## License
|
43
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,7 +1,8 @@
|
|
1
1
|
module ServicePattern
|
2
2
|
path = "#{File.dirname(__FILE__)}/service_pattern"
|
3
3
|
|
4
|
-
autoload :
|
4
|
+
autoload :FailError, "#{path}/fail_error"
|
5
|
+
autoload :FailedError, "#{path}/failed_error"
|
5
6
|
autoload :Response, "#{path}/response"
|
6
7
|
autoload :Service, "#{path}/service"
|
7
8
|
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?
|
@@ -1,29 +1,71 @@
|
|
1
1
|
class ServicePattern::Service
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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)
|
5
|
+
|
6
|
+
can_execute_response = service.can_execute?
|
7
|
+
ServicePattern::Service.fail!(can_execute_response.errors) unless can_execute_response.success?
|
8
|
+
|
9
|
+
service.execute
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.call(*args, &blk)
|
13
|
+
execute(*args, &blk)
|
12
14
|
end
|
13
15
|
|
14
|
-
def self.execute
|
15
|
-
service = new(*args)
|
16
|
+
def self.execute(*args, &blk)
|
17
|
+
service = new(*args, &blk)
|
16
18
|
|
17
19
|
can_execute_response = service.can_execute?
|
18
|
-
|
19
|
-
|
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)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.execute!(*args, &blk)
|
28
|
+
service = new(*args, &blk)
|
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
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.convert_errors(errors)
|
37
|
+
errors = [errors] unless errors.is_a?(Array)
|
38
|
+
errors.map do |error|
|
39
|
+
error = ServicePattern::FailError.new(message: error) unless error.is_a?(ServicePattern::FailError)
|
40
|
+
error
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.fail!(errors)
|
45
|
+
errors = convert_errors(errors)
|
46
|
+
error_messages = errors.map(&:message)
|
47
|
+
|
48
|
+
error = ServicePattern::FailedError.new(error_messages.join(". "))
|
49
|
+
error.errors = errors
|
50
|
+
|
51
|
+
raise error
|
20
52
|
end
|
21
53
|
|
22
54
|
def can_execute?
|
23
|
-
|
55
|
+
succeed!
|
56
|
+
end
|
57
|
+
|
58
|
+
def execute(*_args)
|
59
|
+
raise NoMethodError, "You should implement the `execute` method on your service"
|
60
|
+
end
|
61
|
+
|
62
|
+
def fail!(errors, type: nil)
|
63
|
+
errors = [ServicePattern::FailError.new(message: errors, type: type)] if type
|
64
|
+
|
65
|
+
ServicePattern::Service.fail!(errors)
|
24
66
|
end
|
25
67
|
|
26
|
-
def
|
27
|
-
|
68
|
+
def succeed!(result = nil)
|
69
|
+
ServicePattern::Response.new(result: result)
|
28
70
|
end
|
29
71
|
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.8
|
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: 2020-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ServicePattern for Ruby on Rails.
|
14
14
|
email:
|
@@ -21,7 +21,8 @@ files:
|
|
21
21
|
- README.md
|
22
22
|
- Rakefile
|
23
23
|
- lib/service_pattern.rb
|
24
|
-
- lib/service_pattern/
|
24
|
+
- lib/service_pattern/fail_error.rb
|
25
|
+
- lib/service_pattern/failed_error.rb
|
25
26
|
- lib/service_pattern/response.rb
|
26
27
|
- lib/service_pattern/service.rb
|
27
28
|
- lib/service_pattern/version.rb
|
@@ -45,8 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
46
|
- !ruby/object:Gem::Version
|
46
47
|
version: '0'
|
47
48
|
requirements: []
|
48
|
-
|
49
|
-
rubygems_version: 2.6.8
|
49
|
+
rubygems_version: 3.0.6
|
50
50
|
signing_key:
|
51
51
|
specification_version: 4
|
52
52
|
summary: ServicePattern for Ruby on Rails.
|
@@ -1 +0,0 @@
|
|
1
|
-
class ServicePattern::CantExecuteError < RuntimeError; end
|