service_pattern 0.0.5 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb9549ce95148a91b89260318126e623b02f78c270c33ac4b0e888262419c600
|
4
|
+
data.tar.gz: 64c60eb1773e0af7becbe29bc7d8804776307142f17b0b093ebe9acfd4cf2bfa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8436813bd1329708c015e3e1aec3b51e38f0d09fffcbc6c795b57c236d254467959d9dd626e92dcf669f7ecefba06114e0956eca3759ec54e81b4667cd7f1005
|
7
|
+
data.tar.gz: 9b1919e0993ee2d18892a612a0db0c2e2c4460d23830454d2935c50adb4eed546c057f7b56da39ddcd68f9af17b6867ec7dcc90c09ed7649f32f55c048a4eda9
|
data/lib/service_pattern.rb
CHANGED
@@ -2,11 +2,19 @@ class ServicePattern::Response
|
|
2
2
|
attr_reader :errors, :result
|
3
3
|
|
4
4
|
def initialize(errors: [], result: nil)
|
5
|
-
@errors = errors
|
5
|
+
@errors = ServicePattern::Service.convert_errors(errors)
|
6
6
|
@result = result
|
7
7
|
@success = !errors || errors.empty?
|
8
8
|
end
|
9
9
|
|
10
|
+
def error_messages
|
11
|
+
@error_messages ||= @errors.map(&:message)
|
12
|
+
end
|
13
|
+
|
14
|
+
def error_types
|
15
|
+
@error_types ||= @errors.map(&:type).reject(&:blank?)
|
16
|
+
end
|
17
|
+
|
10
18
|
def success?
|
11
19
|
@success
|
12
20
|
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,26 +14,28 @@ 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
|
-
ServicePattern::Service.fail!(response.errors) unless response.success?
|
30
|
-
response.result
|
21
|
+
new(*args, &blk).execute!
|
31
22
|
end
|
32
23
|
|
33
|
-
def self.
|
24
|
+
def self.convert_errors(errors)
|
34
25
|
errors = [errors] unless errors.is_a?(Array)
|
35
|
-
|
26
|
+
errors.map do |error|
|
27
|
+
error = ServicePattern::FailError.new(message: error) unless error.is_a?(ServicePattern::FailError)
|
28
|
+
error
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.fail!(errors)
|
33
|
+
errors = convert_errors(errors)
|
34
|
+
error_messages = errors.map(&:message)
|
35
|
+
|
36
|
+
error = ServicePattern::FailedError.new(error_messages.join(". "))
|
36
37
|
error.errors = errors
|
38
|
+
|
37
39
|
raise error
|
38
40
|
end
|
39
41
|
|
@@ -41,11 +43,30 @@ class ServicePattern::Service
|
|
41
43
|
succeed!
|
42
44
|
end
|
43
45
|
|
44
|
-
def execute
|
45
|
-
|
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)
|
46
53
|
end
|
47
54
|
|
48
|
-
def
|
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"
|
65
|
+
end
|
66
|
+
|
67
|
+
def fail!(errors, type: nil)
|
68
|
+
errors = [ServicePattern::FailError.new(message: errors, type: type)] if type
|
69
|
+
|
49
70
|
ServicePattern::Service.fail!(errors)
|
50
71
|
end
|
51
72
|
|
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.1
|
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-06-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ServicePattern for Ruby on Rails.
|
14
14
|
email:
|
@@ -21,6 +21,7 @@ files:
|
|
21
21
|
- README.md
|
22
22
|
- Rakefile
|
23
23
|
- lib/service_pattern.rb
|
24
|
+
- lib/service_pattern/fail_error.rb
|
24
25
|
- lib/service_pattern/failed_error.rb
|
25
26
|
- lib/service_pattern/response.rb
|
26
27
|
- lib/service_pattern/service.rb
|
@@ -38,7 +39,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
39
|
requirements:
|
39
40
|
- - ">="
|
40
41
|
- !ruby/object:Gem::Version
|
41
|
-
version:
|
42
|
+
version: 2.5.0
|
42
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
44
|
requirements:
|
44
45
|
- - ">="
|