porch 0.3.0 → 0.3.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
  SHA1:
3
- metadata.gz: d9cef9297ff144b552ab8420b515835966552f80
4
- data.tar.gz: 3af8a85e568ef39b7c7a8dcea322db10771e116b
3
+ metadata.gz: 3d75a6afe5bf346fc7d606bbbd64798a9fcae8bf
4
+ data.tar.gz: 1db71b1a31943f9ec633ba627e1f758d7afd8162
5
5
  SHA512:
6
- metadata.gz: 1872d33f21dcae52e1a899201bf911511e4aeb09c891bbb109712567aa8ed93af03d4f24a2917cb80edbc1d80700066e8ff0005fe6bb106451bcc660954d9fec
7
- data.tar.gz: acdc0dca275b84e6c27a2d2649f1a6ae706fe831fbf0d47354a4e9a2d8f1acc454eebfd598041cd2d84ae982f570bea9f31478580407ec78a10a0c3ba092ec3e
6
+ metadata.gz: f1ad2be642145f3e72c1d00a883020edcee579f92c568d64446db4a192fe3094af2cd33f204c9ddff6b548f3dd923ecf33f648fad87d05d0c6732ca8cdfb9466
7
+ data.tar.gz: 5745ce3d37f53abac0355f5a177dc98679e32cc1b727c5da9cebc366e484a9db6ee3d7e9bfa3ee914375eeab5c89e69c3f90657486e806b9dc8480450c9f860f
data/Gemfile.lock CHANGED
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- porch (0.3.0)
4
+ porch (0.3.1)
5
5
  dry-validation (~> 0.10.4)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- concurrent-ruby (1.0.4)
10
+ concurrent-ruby (1.0.5)
11
11
  diff-lcs (1.2.5)
12
12
  dry-configurable (0.5.0)
13
13
  concurrent-ruby (~> 1.0)
data/README.md CHANGED
@@ -74,28 +74,6 @@ end
74
74
 
75
75
  ### Defining steps or actions
76
76
 
77
- You can define steps as classes and include some nice helper methods. (COMING SOON)
78
-
79
- ```
80
- # app/services/steps/create_user.rb
81
-
82
- require "porch"
83
-
84
- class CreateUser
85
- include Porch::Step
86
-
87
- params do
88
- required(:email).filled(type?: :str, format?: RegEx.email_address)
89
- required(:password).filled(type?: :str, min_size?: 8)
90
- end
91
-
92
- def call(context)
93
- context.user = User.create email: context.email, password: context.password
94
- context.fail! context.user.errors unless context.user.valid?
95
- end
96
- end
97
- ```
98
-
99
77
  You can define steps as PORO classes that respond to a call method.
100
78
 
101
79
  ```
@@ -199,6 +177,40 @@ if result.failure?
199
177
  end
200
178
  ```
201
179
 
180
+ ### Handling/rescuing from errors
181
+
182
+ Errors may be raised within your steps at any point and you may want to handle those errors gracefully.
183
+
184
+ You can use the `rescue_from` error handlers to handle various errors gracefully. The error handler will be called for the type of error or a descendent of the type of error.
185
+
186
+ You can `rescue_from` various errors with a method.
187
+
188
+ ```
189
+ class RegistersUser
190
+ include Porch::Organizer
191
+
192
+ rescue_from [Net::SMTPAuthenticationError, Net::SMTPServerBusy], with: :smtp_error
193
+
194
+ private
195
+
196
+ def smtp_error(exception)
197
+ logger.error exception
198
+ end
199
+ end
200
+ ```
201
+
202
+ You can `rescue_from` various errors with a block.
203
+
204
+ ```
205
+ class RegistersUser
206
+ include Porch::Organizer
207
+
208
+ rescue_from Net::SMTPAuthenticationError do |exception|
209
+ logger.error exception
210
+ end
211
+ end
212
+ ```
213
+
202
214
  ### Skipping steps
203
215
 
204
216
  At any step, you can skip the remaining actions in the organizer. This stops the running of the remaining actions but the `Porch::Context` will still return a successful `Porch::Context`.
data/lib/porch.rb CHANGED
@@ -5,6 +5,7 @@ require "porch/executable_step_decorator"
5
5
  require "porch/human_error"
6
6
  require "porch/guard_rail"
7
7
  require "porch/organizer"
8
+ require "porch/rescuable"
8
9
  require "porch/step_chain"
9
10
  require "porch/version"
10
11
 
@@ -1,3 +1,5 @@
1
+ require_relative "rescuable"
2
+
1
3
  module Porch
2
4
  module Organizer
3
5
  attr_reader :context
@@ -5,10 +7,18 @@ module Porch
5
7
  def with(parameters={})
6
8
  @context = Context.new parameters
7
9
 
8
- chain = StepChain.new(self)
9
- yield chain if block_given?
10
+ handle_exceptions do
11
+ chain = StepChain.new(self)
12
+ yield chain if block_given?
13
+
14
+ chain.execute context
15
+ end
16
+ end
10
17
 
11
- chain.execute context
18
+ def self.included(base)
19
+ base.class_eval do
20
+ include Rescuable
21
+ end
12
22
  end
13
23
  end
14
24
  end
@@ -0,0 +1,52 @@
1
+ module Porch
2
+ module Rescuable
3
+ def handle_exceptions
4
+ yield if block_given?
5
+ rescue Exception => e
6
+ rescue_with_handler(e) || raise(e)
7
+ end
8
+
9
+ def handler_for_rescue(exception)
10
+ _, handler = self.class.rescue_handlers.detect do |exception_class, _|
11
+ exception_class === exception
12
+ end
13
+
14
+ handler
15
+ end
16
+
17
+ def rescue_with_handler(exception)
18
+ handler = handler_for_rescue(exception)
19
+ unless handler.nil?
20
+ case handler
21
+ when Symbol
22
+ self.method(handler).call exception
23
+ when Proc
24
+ self.instance_exec exception, &handler
25
+ end
26
+
27
+ exception
28
+ end
29
+ end
30
+
31
+ module ClassMethods
32
+ def rescue_from(*klasses, with: nil, &block)
33
+ handler = block_given? ? block : with
34
+ raise ArgumentError, \
35
+ "Requires a handler. Use the with keyword argument or supply a block." \
36
+ if handler.nil?
37
+
38
+ klasses.each do |klass|
39
+ rescue_handlers << [klass, handler]
40
+ end
41
+ end
42
+
43
+ def rescue_handlers
44
+ @rescue_handlers ||= []
45
+ end
46
+ end
47
+
48
+ def self.included(base)
49
+ base.extend ClassMethods
50
+ end
51
+ end
52
+ end
data/lib/porch/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Porch
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
Binary file
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: porch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Wright
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-17 00:00:00.000000000 Z
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-validation
@@ -93,6 +93,7 @@ files:
93
93
  - lib/porch/guard_rail/guard.rb
94
94
  - lib/porch/human_error.rb
95
95
  - lib/porch/organizer.rb
96
+ - lib/porch/rescuable.rb
96
97
  - lib/porch/step_chain.rb
97
98
  - lib/porch/step_decorators/class_step_decorator.rb
98
99
  - lib/porch/step_decorators/method_step_decorator.rb
@@ -101,6 +102,8 @@ files:
101
102
  - pkg/porch-0.1.0.gem
102
103
  - pkg/porch-0.2.0.gem
103
104
  - pkg/porch-0.2.1.gem
105
+ - pkg/porch-0.3.0.gem
106
+ - pkg/porch-0.3.1.gem
104
107
  - porch.gemspec
105
108
  homepage: https://github.com/jwright/porch
106
109
  licenses:
@@ -122,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
125
  version: '0'
123
126
  requirements: []
124
127
  rubyforge_project:
125
- rubygems_version: 2.5.1
128
+ rubygems_version: 2.4.5
126
129
  signing_key:
127
130
  specification_version: 4
128
131
  summary: A simple service layer pattern for plain old Ruby objects.