service_pattern 0.0.4 → 0.0.5

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: eb37f2c1aa001cef4abdfe0937e15b82c52bb845d44c9527851bc6d770bfd44d
4
- data.tar.gz: 52344b2c5450cb137bcb743c4fe24bf543e5c22c8d61a831a063d5a4b5de7829
3
+ metadata.gz: 278a1fa25b8d796da5bbd34a1b9f25f4de20bc11e4567290284c04706955e67f
4
+ data.tar.gz: 839b2968eeecfcce9319b4730eb6b9f58a0fca4529c8a56c063a588af9d35812
5
5
  SHA512:
6
- metadata.gz: 432e00eceea33e9f3335bcc00a5fc44f153ddbf925e11b853d3e6c698607bac1f090a6060eaa88875c5535d86863212409426b28a7ae5a1c66ad42991ceae1ca
7
- data.tar.gz: a072a0f461b12c9e09e7a9d2c80ee593015434797cb6da32ef7825f0ab1c834e6d77caf22cc36ad264f52950980931cef6b7a6de3930a4d53280b2ef563673aa
6
+ metadata.gz: f8b6f769bb27497d09a37956659e793ebfd1a57fdfa175070fd0ae731ac60e2738c213c0d3a979cd8c4448fe533dfe7f3aaaea506fe0918e156e7f8a6e29f957
7
+ data.tar.gz: c3e7c12b8d3e5bfce1f963e58dd26e0a540b0594deb2bc94fb5b1c46cb196e2424e2f279601f700b30b856bd1022eab818171d20ab9008e64b6cb52d7af51d12
data/README.md CHANGED
@@ -23,7 +23,7 @@ Create your first service in "app/services/users/activator_service":
23
23
  class Users::ActivatorService < ApplicationService
24
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,17 +33,35 @@ 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
+
42
60
  ### Returning results
43
61
 
44
62
  You can also return a result, which will automatically make the response successfull:
45
63
  ```ruby
46
- ServicePattern::Response.new(result: {message: "Hello world"})
64
+ succeed!(message: "Hello world")
47
65
  ```
48
66
 
49
67
  You can then retrieve it like this:
@@ -52,5 +70,15 @@ response = Users::ActivatorService.()
52
70
  puts "Result: #{response.result}"
53
71
  ```
54
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
+
55
83
  ## License
56
84
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -1 +1,3 @@
1
- class ServicePattern::FailedError < RuntimeError; end
1
+ class ServicePattern::FailedError < RuntimeError
2
+ attr_accessor :errors
3
+ end
@@ -1,19 +1,10 @@
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]
7
-
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
4
+ def initialize(errors: [], result: nil)
5
+ @errors = errors
6
+ @result = result
7
+ @success = !errors || errors.empty?
17
8
  end
18
9
 
19
10
  def success?
@@ -4,7 +4,7 @@ class ServicePattern::Service
4
4
  service = new(*args, &blk)
5
5
 
6
6
  can_execute_response = service.can_execute?
7
- raise ServicePattern::FailedError, can_execute_response.errors.join(". ") unless can_execute_response.success?
7
+ ServicePattern::Service.fail!(can_execute_response.errors) unless can_execute_response.success?
8
8
 
9
9
  service.execute
10
10
  end
@@ -21,21 +21,35 @@ class ServicePattern::Service
21
21
 
22
22
  service.execute
23
23
  rescue ServicePattern::FailedError => e
24
- ServicePattern::Response.new(errors: [e.message])
24
+ ServicePattern::Response.new(errors: e.errors)
25
25
  end
26
26
 
27
27
  def self.execute!(*args, &blk)
28
28
  response = execute(*args, &blk)
29
- raise ServicePattern::FailedError, response.errors.join(". ") unless response.success?
30
-
29
+ ServicePattern::Service.fail!(response.errors) unless response.success?
31
30
  response.result
32
31
  end
33
32
 
33
+ def self.fail!(errors)
34
+ errors = [errors] unless errors.is_a?(Array)
35
+ error = ServicePattern::FailedError.new(errors.join(". "))
36
+ error.errors = errors
37
+ raise error
38
+ end
39
+
34
40
  def can_execute?
35
- ServicePattern::Response.new(success: true)
41
+ succeed!
36
42
  end
37
43
 
38
44
  def execute(*_args)
39
- raise NoMethodError, "You should implement the `execute!` method on your service"
45
+ raise NoMethodError, "You should implement the `execute` method on your service"
46
+ end
47
+
48
+ def fail!(errors)
49
+ ServicePattern::Service.fail!(errors)
50
+ end
51
+
52
+ def succeed!(result = nil)
53
+ ServicePattern::Response.new(result: result)
40
54
  end
41
55
  end
@@ -1,3 +1,3 @@
1
1
  module ServicePattern
2
- VERSION = "0.0.4".freeze
2
+ VERSION = "0.0.5".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.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-19 00:00:00.000000000 Z
11
+ date: 2019-12-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ServicePattern for Ruby on Rails.
14
14
  email: