pipe-ruby 0.2.0 → 0.3.1

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
  SHA1:
3
- metadata.gz: 3360d89c8d7925a9c69a3fe6e0549722890e1235
4
- data.tar.gz: b95e816b5f0a74e3712f3553f8c91f00bb93a0ec
3
+ metadata.gz: d0eca86ffad3466f3843db7cb3013985bfc1c3bf
4
+ data.tar.gz: df2cd7013334b5abce3608c1c85bcd4ca261201a
5
5
  SHA512:
6
- metadata.gz: 87cf0cbe09d6660cb55ff502f19d7acf6cac4eb09aa7c89c94e72432fdb8f8cf29df32a9778ff0989f61c837b514c77da8f4868404a2ec039b6af271fcf1e87a
7
- data.tar.gz: 221cd32905cfc0081a6b99f27071811140ef5e18800dd6fa46a0f745f46da451d8f719fad15a3087c7994b5a51990878deba4ecea4a87169b8288102d3785809
6
+ metadata.gz: 918395735261b1110693cfa0c08d933e5c7c8ea5e0a73597a3d067a5fc3b29044f36a47b608dfb5a0c911f9df1f3711ff50fbf765bb6a960b91bff22670a30f8
7
+ data.tar.gz: 9338db4f769305be5ad63474cca4e4e2ef2fcfebee1f8eb0ade7a2231f992beebfe6e92d842e31be5e823c1d208ba109c911cd72c63f944048214b785d342bf8
data/README.md CHANGED
@@ -55,12 +55,17 @@ configurable options. Here they are with their defaults:
55
55
 
56
56
  ```ruby
57
57
  Pipe::Config.new(
58
- :error_handlers => [], # an array of procs to be called when an error occurs
59
- :raise_on_error => true, # tells Pipe to re-raise errors which occur
60
- :skip_on => false, # a truthy value or proc which tells pipe to skip the
61
- # next method in the `through` array
62
- :stop_on => false # a truthy value or proc which tells pipe to stop
63
- # processing and return the current value
58
+ :error_handlers => [], # an array of procs to be called when an error
59
+ # occurs
60
+ :raise_on_error => true, # tells Pipe to re-raise errors which occur
61
+ :return_on_error => :subject, # when an error happens and raise error is false
62
+ # returns the current value of subject defaultly;
63
+ # if callable, will return the result of the call
64
+ # if not callable, will return the value
65
+ :skip_on => false, # a truthy value or proc which tells pipe to skip
66
+ # the next method in the `through` array
67
+ :stop_on => false # a truthy value or proc which tells pipe to stop
68
+ # processing and return the current value
64
69
  )
65
70
  ```
66
71
 
@@ -1,16 +1,18 @@
1
1
  module Pipe
2
2
  class Config
3
3
  attr_accessor :raise_on_error
4
- attr_reader :error_handlers, :skip_on, :stop_on
4
+ attr_reader :error_handlers, :return_on_error, :skip_on, :stop_on
5
5
 
6
6
  def initialize(
7
7
  error_handlers: [],
8
8
  raise_on_error: true,
9
+ return_on_error: :subject,
9
10
  skip_on: false,
10
11
  stop_on: false
11
12
  )
12
13
  @error_handlers = error_handlers
13
14
  @raise_on_error = raise_on_error
15
+ @return_on_error = return_on_error
14
16
  self.skip_on = skip_on
15
17
  self.stop_on = stop_on
16
18
  end
@@ -14,8 +14,9 @@ module Pipe
14
14
 
15
15
  process(subj, method)
16
16
  rescue => e
17
- handle_error(:error => e, :method => method, :subject => subj)
18
- break subj
17
+ payload = {:error => e, :method => method, :subject => subj}
18
+ handle_error(payload)
19
+ break error_response(payload)
19
20
  end
20
21
  }
21
22
  end
@@ -24,6 +25,16 @@ module Pipe
24
25
 
25
26
  attr_accessor :config, :context, :subject, :through
26
27
 
28
+ def error_response(error:, method:, subject:)
29
+ if config.return_on_error == :subject
30
+ subject
31
+ elsif config.return_on_error.respond_to?(:call)
32
+ config.return_on_error.call(subject, method, error)
33
+ else
34
+ config.return_on_error
35
+ end
36
+ end
37
+
27
38
  def handle_error(error:, method:, subject:)
28
39
  process_error_handlers(
29
40
  :error => error,
@@ -1,3 +1,3 @@
1
1
  module Pipe
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -10,6 +10,10 @@ describe Pipe::Config do
10
10
  expect(Pipe::Config.new.raise_on_error).to eq(true)
11
11
  end
12
12
 
13
+ it "sets return_on_error to :subject" do
14
+ expect(Pipe::Config.new.return_on_error).to eq(:subject)
15
+ end
16
+
13
17
  it "sets skip_on to a proc which returns false" do
14
18
  expect(Pipe::Config.new.skip_on.call).to eq(false)
15
19
  end
@@ -119,10 +119,10 @@ describe Pipe::Reducer do
119
119
  context = Class.new do
120
120
  ExpectedError = Class.new(StandardError)
121
121
 
122
- def bomb
122
+ def bomb(subject)
123
123
  raise ExpectedError, "BOOM!"
124
124
  end
125
- end
125
+ end.new
126
126
 
127
127
  expect(handler1).to receive(:call)
128
128
  expect(handler2).to receive(:call)
@@ -142,10 +142,10 @@ describe Pipe::Reducer do
142
142
  context = Class.new do
143
143
  AnotherExpectedError = Class.new(StandardError)
144
144
 
145
- def bomb
145
+ def bomb(subject)
146
146
  raise AnotherExpectedError, "BOOM!"
147
147
  end
148
- end
148
+ end.new
149
149
 
150
150
  expect{
151
151
  Pipe::Reducer.new(
@@ -156,5 +156,76 @@ describe Pipe::Reducer do
156
156
  ).reduce
157
157
  }.to_not raise_error
158
158
  end
159
+
160
+ it "honors Config#return_on_error :subject default" do
161
+ config = Pipe::Config.new(:raise_on_error => false)
162
+ context = Class.new do
163
+ AndAnotherExpectedError = Class.new(StandardError)
164
+
165
+ def bomb(subject)
166
+ raise AndAnotherExpectedError, "BOOM!"
167
+ end
168
+ end.new
169
+ subject = Object.new
170
+
171
+ expect(
172
+ Pipe::Reducer.new(
173
+ :config => config,
174
+ :context => context,
175
+ :subject => subject,
176
+ :through => [:bomb]
177
+ ).reduce
178
+ ).to eq(subject)
179
+ end
180
+
181
+ it "honors Config#return_on_error callable objects" do
182
+ subject = Object.new
183
+ config = Pipe::Config.new(
184
+ :raise_on_error => false,
185
+ :return_on_error => proc { |subj, method, e|
186
+ [subj, method, e.class, e.to_s, :hello]
187
+ }
188
+ )
189
+ context = Class.new do
190
+ YetAnotherExpectedError = Class.new(StandardError)
191
+
192
+ def bomb(subject)
193
+ raise YetAnotherExpectedError, "BOOM!"
194
+ end
195
+ end.new
196
+ expected = [subject, :bomb, YetAnotherExpectedError, "BOOM!", :hello]
197
+
198
+ expect(
199
+ Pipe::Reducer.new(
200
+ :config => config,
201
+ :context => context,
202
+ :subject => subject,
203
+ :through => [:bomb]
204
+ ).reduce
205
+ ).to eq(expected)
206
+ end
207
+
208
+ it "returns Config#return_on_error when not callable" do
209
+ config = Pipe::Config.new(
210
+ :raise_on_error => false,
211
+ :return_on_error => :error
212
+ )
213
+ context = Class.new do
214
+ OneMoreError = Class.new(StandardError)
215
+
216
+ def bomb(subject)
217
+ raise OneMoreError, "BOOM!"
218
+ end
219
+ end.new
220
+
221
+ expect(
222
+ Pipe::Reducer.new(
223
+ :config => config,
224
+ :context => context,
225
+ :subject => Object.new,
226
+ :through => [:bomb]
227
+ ).reduce
228
+ ).to eq(:error)
229
+ end
159
230
  end
160
231
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pipe-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Matthews
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-28 00:00:00.000000000 Z
11
+ date: 2015-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler