service_pattern 0.0.7 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77863da179021d925441d0bf4a64fe8756ce925c45262523e7e34ff35e03956b
4
- data.tar.gz: 815cac6f2393f6f92181a14fd311c565d63069e96c9aebc3827c315fa24d2832
3
+ metadata.gz: 4a0cc423721d1d11a5e300f3719b01dcd3eba89dcad83d97cc168372acc01593
4
+ data.tar.gz: de7dcffe6dd6ac6bda7d5195e75622ca1bda3c37b3cca394ca11e2a827445da6
5
5
  SHA512:
6
- metadata.gz: 370ba577ae9fc3a77746e518502c71d872e22ee49ddab016124c1c3105a48b8cc6667e22ea4f9369f4224b9de298c3ff04940031eb86aeb1e8d9f8890fcb0289
7
- data.tar.gz: a80e81f4419b97fa4721a747a180729484264635d4feff4edb572d5e4dabda6a11d86928d1ba5fb1fcf11c9bd94734d82c9449bce4c8ee7d762af3c5e47de19c
6
+ metadata.gz: c304984c8080f2234c44cb07f96566f8830ca7a17e1564f76f7d5f52183dd0317c4584dc36a990ae6a7a97f77e1719d8fcb3fb36463be9e62c694d6e69521210
7
+ data.tar.gz: 0a168abccb3b302f15987e67515214e16565e4ba654f8c7079131eeeb2cbbb2ddeb3976ce4b0645215278df8c0f180feeaba9f36eb511d796624f22823a79eaa
@@ -2,7 +2,7 @@ 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
@@ -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.execute
9
+ service.perform
10
10
  end
11
11
 
12
12
  def self.call(*args, &blk)
@@ -14,32 +14,23 @@ class ServicePattern::Service
14
14
  end
15
15
 
16
16
  def self.execute(*args, &blk)
17
- service = new(*args, &blk)
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
- 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
21
+ new(*args, &blk).execute!
34
22
  end
35
23
 
36
- def self.fail!(errors)
24
+ def self.convert_errors(errors)
37
25
  errors = [errors] unless errors.is_a?(Array)
38
- errors = errors.map do |error|
26
+ errors.map do |error|
39
27
  error = ServicePattern::FailError.new(message: error) unless error.is_a?(ServicePattern::FailError)
40
28
  error
41
29
  end
30
+ end
42
31
 
32
+ def self.fail!(errors)
33
+ errors = convert_errors(errors)
43
34
  error_messages = errors.map(&:message)
44
35
 
45
36
  error = ServicePattern::FailedError.new(error_messages.join(". "))
@@ -52,8 +43,25 @@ class ServicePattern::Service
52
43
  succeed!
53
44
  end
54
45
 
55
- def execute(*_args)
56
- raise NoMethodError, "You should implement the `execute` method on your service"
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"
57
65
  end
58
66
 
59
67
  def fail!(errors, type: nil)
@@ -1,3 +1,3 @@
1
1
  module ServicePattern
2
- VERSION = "0.0.7".freeze
2
+ VERSION = "1.0.2".freeze
3
3
  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.7
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-02 00:00:00.000000000 Z
11
+ date: 2021-10-04 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: '0'
42
+ version: 2.5.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.0.6
49
+ rubygems_version: 3.1.6
50
50
  signing_key:
51
51
  specification_version: 4
52
52
  summary: ServicePattern for Ruby on Rails.