service_pattern 0.0.6 → 0.0.7

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: c422a0eeb31fd82401662b38f5bf20e8842919a61b9c6ac9679d143284142d8b
4
- data.tar.gz: 6b4ec9a0abb29917fc473d92ef99e4b8440832afe68e3f2c8387bb8b2a61f70b
3
+ metadata.gz: 77863da179021d925441d0bf4a64fe8756ce925c45262523e7e34ff35e03956b
4
+ data.tar.gz: 815cac6f2393f6f92181a14fd311c565d63069e96c9aebc3827c315fa24d2832
5
5
  SHA512:
6
- metadata.gz: 6bce5fc37731897aca940277f54165521d2fc0dd5b0b827abfc6a4a3b720cd2248f1ad68fd46bf2288907aac842df1f5681378d04695db4bfd9383d2f02e7abe
7
- data.tar.gz: 29f21381d0169db901e5e547e1a538207ef57d1a0f580e1572b8a0256e56a4bf424cc72f99254149923a628003ff515407055c29bb86cdd75a86126f8a40d8ab
6
+ metadata.gz: 370ba577ae9fc3a77746e518502c71d872e22ee49ddab016124c1c3105a48b8cc6667e22ea4f9369f4224b9de298c3ff04940031eb86aeb1e8d9f8890fcb0289
7
+ data.tar.gz: a80e81f4419b97fa4721a747a180729484264635d4feff4edb572d5e4dabda6a11d86928d1ba5fb1fcf11c9bd94734d82c9449bce4c8ee7d762af3c5e47de19c
@@ -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
@@ -7,6 +7,14 @@ class ServicePattern::Response
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
@@ -35,8 +35,16 @@ class ServicePattern::Service
35
35
 
36
36
  def self.fail!(errors)
37
37
  errors = [errors] unless errors.is_a?(Array)
38
- error = ServicePattern::FailedError.new(errors.join(". "))
38
+ errors = errors.map do |error|
39
+ error = ServicePattern::FailError.new(message: error) unless error.is_a?(ServicePattern::FailError)
40
+ error
41
+ end
42
+
43
+ error_messages = errors.map(&:message)
44
+
45
+ error = ServicePattern::FailedError.new(error_messages.join(". "))
39
46
  error.errors = errors
47
+
40
48
  raise error
41
49
  end
42
50
 
@@ -48,7 +56,9 @@ class ServicePattern::Service
48
56
  raise NoMethodError, "You should implement the `execute` method on your service"
49
57
  end
50
58
 
51
- def fail!(errors)
59
+ def fail!(errors, type: nil)
60
+ errors = [ServicePattern::FailError.new(message: errors, type: type)] if type
61
+
52
62
  ServicePattern::Service.fail!(errors)
53
63
  end
54
64
 
@@ -1,3 +1,3 @@
1
1
  module ServicePattern
2
- VERSION = "0.0.6".freeze
2
+ VERSION = "0.0.7".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.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-18 00:00:00.000000000 Z
11
+ date: 2020-09-02 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