simple_pipeline 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
  SHA1:
3
- metadata.gz: '0841cc3eab093abcc6a954e3d24c1153de824d02'
4
- data.tar.gz: 46f0b23b29b62843bbefbe73615ac318c1082214
3
+ metadata.gz: 677d2ff5729eb15c9fd13f634606d462279001de
4
+ data.tar.gz: 781a8d2a9772234ce73be2ba1d42c87b75ff6ecc
5
5
  SHA512:
6
- metadata.gz: dc93171e985aa842a237ddce20c0ab935d5bcb776a63153e3ca6429018435e8cae0dff833641ea6c39b6c6184d838df00d26610707b80acd731ec762cbe20015
7
- data.tar.gz: eb66c3a879ef980ab1679a8e6b5150cc9c0680ac85d6664a1f5d255f40e92fa9a051d4771116a9bc956f143c1459c1657c28e478c36125c6356ed3a78486d143
6
+ metadata.gz: 4545e19ba08add3987a1cd729c478917098d754df9557a3fe79f15309df96fa40a71c4a6ffe9fc5b8f0c20c68bded8a00adb3f7800ebf617ef9d69e8b9499e56
7
+ data.tar.gz: 84e0c38fa8984b5843c861d6f5fcffd3ed4053cce508273d4b6fb483b680b0045c7c8ef5a0e7a3e85041095bde656f5541eb7d692104ffce20f192af2b770437
data/README.md CHANGED
@@ -58,7 +58,8 @@ You can use the **SimplePipeline::Timeout** mixin to enforce a timeout value (in
58
58
  class TimeoutPipe
59
59
  include SimplePipeline::Timeout
60
60
 
61
- set_timeout 3 # Set the timeout value to be three seconds
61
+ # Set the timeout value to be 3 seconds
62
+ set_timeout 3
62
63
 
63
64
  def process (payload)
64
65
  # Do something
@@ -74,21 +75,48 @@ payload = {:some_key => some_value}
74
75
  pipeline.process payload
75
76
  ```
76
77
 
77
- You can also set the timeout value on a per instance basis.
78
+ You can also set the timeout value on a per instance basis. This will override the timeout value set by the class definition.
78
79
 
79
80
  ```ruby
80
- pipeline = SimplePipeline.new
81
-
82
81
  pipe1 = TimeoutPipe.new
83
- pipe1.set_timeout 10
82
+ pipe1.set_timeout 10 # seconds
84
83
 
85
84
  pipe2 = TimeoutPipe.new
86
- pipe2.set_timeout 60
85
+ pipe2.set_timeout 60 # seconds
87
86
 
88
87
  pipeline.add pipe1
89
88
  pipeline.add pipe2
90
89
  ```
91
- This will override the timeout value set by the class definition.
90
+
91
+ If you don't want to use the **SimplePipeline::Timeout** mixin for your pipe, you can still set a timeout by passing in a ```:timeout``` value when you are adding the pipe. If you do this, the param value will take precedence over any other timeout value set by the mixin.
92
+
93
+ ```ruby
94
+ # Timeout value set to 10 seconds, even though SomePipe doesn't include SimplePipeline::Timeout
95
+ pipeline.add SomePipe.new, :timeout => 10
96
+
97
+ # Timeout value set to 10 seconds, even though TimeoutPipe defaults to a timeout of 3 seconds
98
+ pipeline.add TimeoutPipe.new, :timeout => 10
99
+ ```
100
+
101
+ ## Exception Handling
102
+
103
+ By default, execution of the entire pipeline will halt if any of the pipes raise a ```StandardError```. However, this can be overriden using the ```:continue_on_error?``` parameter.
104
+
105
+ ```ruby
106
+ # Pipeline continues executing if any kind of StandardError is encountered
107
+ pipeline.add pipe, :continue_on_error? => true
108
+
109
+ # Pipeline continues executing if NameError or subclass (e.g. NoMethodError) is encountered
110
+ pipeline.add pipe, :continue_on_error? => NameError
111
+
112
+ # Pipeline continues executing on either ArgumentError or NameError (or subclass)
113
+ pipeline.add pipe, :continue_on_error? => [ArgumentError, NameError]
114
+
115
+ # Pipeline continues executing if any kind of Exception is encountered - not recommended
116
+ pipeline.add pipe, :continue_on_error? => Exception
117
+ ```
118
+
119
+ After the pipeline finishes executing, you can call ```pipeline.errors``` to get an Array of errors that were caught during execution.
92
120
 
93
121
  ## Related Projects
94
122
 
@@ -1,3 +1,3 @@
1
1
  class SimplePipeline
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -12,15 +12,17 @@ class SimplePipeline
12
12
  @errors = []
13
13
  end
14
14
 
15
- def add (pipe, args = {})
15
+ def add (pipe, params = {})
16
+ process_method = params[:process_method] || :process
17
+
16
18
  begin
17
- raise ArgumentError, "invalid pipe - incorrect number of arguments for process() method (should be 1)" unless pipe.class.instance_method(:process).arity == 1
19
+ raise ArgumentError, "invalid pipe - incorrect number of arguments for #{process_method}() method (should be 1)" unless pipe.class.instance_method(process_method).arity == 1
18
20
  rescue NameError
19
- raise ArgumentError, "invalid pipe - process() method not found"
21
+ raise ArgumentError, "invalid pipe - #{process_method}() method not found"
20
22
  end
21
23
 
22
24
  @pipe_order << pipe
23
- @pipe_config[pipe] = args
25
+ @pipe_config[pipe] = params
24
26
 
25
27
  return pipe
26
28
  end
@@ -32,19 +34,9 @@ class SimplePipeline
32
34
  def process (payload)
33
35
  @pipe_order.each do |pipe|
34
36
  begin
35
- timeout = @pipe_config[pipe][:timeout]
36
-
37
- if timeout.nil? && (pipe.is_a? SimplePipeline::Timeout)
38
- timeout = pipe.timeout
39
- end
40
-
41
- if timeout.nil?
42
- pipe.process(payload)
43
- else
44
- process_with_timeout(pipe, payload, timeout)
45
- end
37
+ invoke_process_with_timeout(pipe, payload, get_timeout(pipe))
46
38
  rescue
47
- raise $! unless continue_on_error?($!, @pipe_config[pipe][:continue_on_error?])
39
+ raise $! unless continue_on_error?($!, pipe)
48
40
  @errors << $!
49
41
  end
50
42
  end
@@ -54,7 +46,9 @@ class SimplePipeline
54
46
 
55
47
  private
56
48
 
57
- def continue_on_error? (e, config_value)
49
+ def continue_on_error? (e, pipe)
50
+ config_value = @pipe_config[pipe][:continue_on_error?]
51
+
58
52
  return false if config_value.nil? || config_value == false
59
53
  return true if config_value == true
60
54
 
@@ -69,10 +63,19 @@ class SimplePipeline
69
63
  return false
70
64
  end
71
65
 
72
-
73
- def process_with_timeout (pipe, payload, timeout)
66
+ def invoke_process_with_timeout (pipe, payload, timeout)
74
67
  ::Timeout::timeout(timeout) {
75
- pipe.process(payload)
68
+ pipe.__send__(get_process_method(pipe), payload)
76
69
  }
77
70
  end
71
+
72
+ def get_process_method (pipe)
73
+ @pipe_config[pipe][:process_method] || :process
74
+ end
75
+
76
+ def get_timeout (pipe)
77
+ timeout = @pipe_config[pipe][:timeout]
78
+ timeout = pipe.timeout if timeout.nil? && (pipe.is_a? SimplePipeline::Timeout)
79
+ return timeout
80
+ end
78
81
  end
@@ -1,18 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
- class TestPipe
3
+ class TestPipe1
4
4
  def process (payload)
5
5
  payload[:test_value] *= 10
6
6
  end
7
7
  end
8
8
 
9
- class BadPipe
9
+ class TestPipe2
10
10
  def process (payload1, payload2)
11
11
  # do nothing
12
12
  end
13
+
14
+ def alternate_process_method (payload)
15
+ payload[:test_value] *= 10
16
+ end
13
17
  end
14
18
 
15
- class TimeoutPipe
19
+ class TimeoutPipe1
16
20
  include SimplePipeline::Timeout
17
21
 
18
22
  set_timeout 3 # seconds
@@ -37,9 +41,9 @@ end
37
41
  describe SimplePipeline do
38
42
  it "should support three normal pipes" do
39
43
  pipeline = SimplePipeline.new
40
- pipeline.add TestPipe.new
41
- pipeline.add TestPipe.new
42
- pipeline.add TestPipe.new
44
+ pipeline.add TestPipe1.new
45
+ pipeline.add TestPipe1.new
46
+ pipeline.add TestPipe1.new
43
47
 
44
48
  payload = {:test_value => 10}
45
49
 
@@ -57,13 +61,13 @@ describe SimplePipeline do
57
61
  }.to raise_error(ArgumentError)
58
62
 
59
63
  expect {
60
- pipeline.add BadPipe
64
+ pipeline.add TestPipe2.new
61
65
  }.to raise_error(ArgumentError)
62
66
  end
63
67
 
64
68
  it "should support pipes with timeout values" do
65
69
  pipeline = SimplePipeline.new
66
- pipe = TimeoutPipe.new
70
+ pipe = TimeoutPipe1.new
67
71
  pipeline.add pipe
68
72
 
69
73
  expect(pipe.timeout).to eq 3
@@ -82,11 +86,11 @@ describe SimplePipeline do
82
86
  }.to raise_error(Timeout::Error)
83
87
  end
84
88
 
85
- it "should support pipes with continue_on_error => true" do
89
+ it "should support pipes with :continue_on_error" do
86
90
  pipeline = SimplePipeline.new
87
- pipeline.add TestPipe.new
91
+ pipeline.add TestPipe1.new
88
92
  pipeline.add ExceptionPipe.new
89
- pipeline.add TestPipe.new
93
+ pipeline.add TestPipe1.new
90
94
 
91
95
  payload = {:test_value => 10}
92
96
 
@@ -98,13 +102,13 @@ describe SimplePipeline do
98
102
  expect(pipeline.errors.size).to eq 0
99
103
 
100
104
  pipeline = SimplePipeline.new
101
- pipeline.add TestPipe.new
105
+ pipeline.add TestPipe1.new
102
106
  pipeline.add ExceptionPipe.new, :continue_on_error? => true
103
107
  pipeline.add ExceptionPipe.new, :continue_on_error? => ArgumentError
104
108
  pipeline.add ExceptionPipe.new, :continue_on_error? => StandardError
105
109
  pipeline.add ExceptionPipe.new, :continue_on_error? => [ArgumentError]
106
110
  pipeline.add ExceptionPipe.new, :continue_on_error? => [RuntimeError, StandardError]
107
- pipeline.add TestPipe.new
111
+ pipeline.add TestPipe1.new
108
112
 
109
113
  payload = {:test_value => 10}
110
114
 
@@ -113,4 +117,19 @@ describe SimplePipeline do
113
117
  expect(payload[:test_value]).to eq 1000
114
118
  expect(pipeline.errors.size).to eq 5
115
119
  end
120
+
121
+ it "should support pipes with :process_method" do
122
+ pipeline = SimplePipeline.new
123
+ pipeline.add TestPipe2.new, :process_method => :alternate_process_method
124
+
125
+ expect {
126
+ pipeline.add TestPipe1.new, :process_method => :this_method_doesnt_exist
127
+ }.to raise_error(ArgumentError)
128
+
129
+ payload = {:test_value => 10}
130
+
131
+ pipeline.process(payload)
132
+
133
+ expect(payload[:test_value]).to eq 100
134
+ end
116
135
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_pipeline
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
  - Jonathan Wong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-04 00:00:00.000000000 Z
11
+ date: 2016-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec