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: 278a1fa25b8d796da5bbd34a1b9f25f4de20bc11e4567290284c04706955e67f
4
- data.tar.gz: 839b2968eeecfcce9319b4730eb6b9f58a0fca4529c8a56c063a588af9d35812
3
+ metadata.gz: fb9549ce95148a91b89260318126e623b02f78c270c33ac4b0e888262419c600
4
+ data.tar.gz: 64c60eb1773e0af7becbe29bc7d8804776307142f17b0b093ebe9acfd4cf2bfa
5
5
  SHA512:
6
- metadata.gz: f8b6f769bb27497d09a37956659e793ebfd1a57fdfa175070fd0ae731ac60e2738c213c0d3a979cd8c4448fe533dfe7f3aaaea506fe0918e156e7f8a6e29f957
7
- data.tar.gz: c3e7c12b8d3e5bfce1f963e58dd26e0a540b0594deb2bc94fb5b1c46cb196e2424e2f279601f700b30b856bd1022eab818171d20ab9008e64b6cb52d7af51d12
6
+ metadata.gz: 8436813bd1329708c015e3e1aec3b51e38f0d09fffcbc6c795b57c236d254467959d9dd626e92dcf669f7ecefba06114e0956eca3759ec54e81b4667cd7f1005
7
+ data.tar.gz: 9b1919e0993ee2d18892a612a0db0c2e2c4460d23830454d2935c50adb4eed546c057f7b56da39ddcd68f9af17b6867ec7dcc90c09ed7649f32f55c048a4eda9
@@ -1,6 +1,7 @@
1
1
  module ServicePattern
2
2
  path = "#{File.dirname(__FILE__)}/service_pattern"
3
3
 
4
+ autoload :FailError, "#{path}/fail_error"
4
5
  autoload :FailedError, "#{path}/failed_error"
5
6
  autoload :Response, "#{path}/response"
6
7
  autoload :Service, "#{path}/service"
@@ -0,0 +1,8 @@
1
+ class ServicePattern::FailError
2
+ attr_reader :message, :type
3
+
4
+ def initialize(message:, type: nil)
5
+ @message = message
6
+ @type = type
7
+ end
8
+ end
@@ -1,3 +1,11 @@
1
1
  class ServicePattern::FailedError < RuntimeError
2
2
  attr_accessor :errors
3
+
4
+ def error_messages
5
+ @error_messages ||= @errors.map(&:message)
6
+ end
7
+
8
+ def error_types
9
+ @error_types ||= @errors.map(&:type)
10
+ end
3
11
  end
@@ -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.execute
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
- 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
- response = execute(*args, &blk)
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.fail!(errors)
24
+ def self.convert_errors(errors)
34
25
  errors = [errors] unless errors.is_a?(Array)
35
- error = ServicePattern::FailedError.new(errors.join(". "))
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(*_args)
45
- 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)
46
53
  end
47
54
 
48
- def fail!(errors)
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
 
@@ -1,3 +1,3 @@
1
1
  module ServicePattern
2
- VERSION = "0.0.5".freeze
2
+ VERSION = "1.0.1".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.5
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: 2019-12-31 00:00:00.000000000 Z
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: '0'
42
+ version: 2.5.0
42
43
  required_rubygems_version: !ruby/object:Gem::Requirement
43
44
  requirements:
44
45
  - - ">="