service_pattern 0.0.3 → 0.0.8

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
- SHA1:
3
- metadata.gz: 8d475628b9f7d75075a9069a45e64849402685d7
4
- data.tar.gz: 06c1e5aa4f668b258a753503c74edaaa30dcf858
2
+ SHA256:
3
+ metadata.gz: 8ba006abbb363e6e905b04bc28d034d3ce3500a1972569796b3a4535d399f2a5
4
+ data.tar.gz: d6843c0cba4fcfe46ea34dc4b7e650a9356566a4e4c6ce2703234a3f44b88ca9
5
5
  SHA512:
6
- metadata.gz: ec2869750e398a401e478bc755f8d1a351f1264ffa5be853e5e79bf0441bc23f11bf4f0abd7dd5446ed222a16e7eda9148d049df013d0a0ffac7557d6c8e3ed6
7
- data.tar.gz: a77b1ad0406d8693ab31397ba307b306f627120baf0dd9ee09ca7eccbd68987b72a9b53371c0aa965797275b00bab5ef1004c3ba0dee4e80ef62d67c0c4f7a9c
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
- ServicePattern::Response.new(success: true)
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 "Wee"
36
+ puts "Result: #{response.result}"
37
37
  else
38
- puts "Errors: #{result.errors.join(". ")}"
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).
@@ -1,7 +1,8 @@
1
1
  module ServicePattern
2
2
  path = "#{File.dirname(__FILE__)}/service_pattern"
3
3
 
4
- autoload :CantExecuteError, "#{path}/cant_execute_error"
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
@@ -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
@@ -0,0 +1,11 @@
1
+ class ServicePattern::FailedError < RuntimeError
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
11
+ end
@@ -1,19 +1,18 @@
1
1
  class ServicePattern::Response
2
2
  attr_reader :errors, :result
3
3
 
4
- def initialize(args)
5
- @errors = args[:errors] || []
6
- @result = args[: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
- if args.key?(:success)
9
- @success = args.fetch(:success)
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
- def self.call(*args)
3
- service = new(*args)
4
-
5
- begin
6
- can_execute_response = service.can_execute?
7
- return can_execute_response unless can_execute_response.success?
8
- service.execute!
9
- rescue => e
10
- ServicePattern::Response.new(errors: ["#{e.class.name}: #{e.message}"], success: false)
11
- end
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!(*args)
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
- raise ServicePattern::CantExecuteError, can_execute_response.errors.join(". ") unless can_execute_response.success?
19
- service.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)
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
- ServicePattern::Response.new(success: true)
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 execute!(*_args)
27
- raise NoMethodError, "You should implement the `execute!` method on your service"
68
+ def succeed!(result = nil)
69
+ ServicePattern::Response.new(result: result)
28
70
  end
29
71
  end
@@ -1,3 +1,3 @@
1
1
  module ServicePattern
2
- VERSION = "0.0.3".freeze
2
+ VERSION = "0.0.8".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.3
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: 2017-07-21 00:00:00.000000000 Z
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/cant_execute_error.rb
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
- rubyforge_project:
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