simple_pipeline 0.0.3 → 0.0.4

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: c4919357b1935895facfb4607f74def1253c1dea
4
- data.tar.gz: 5dace42d21950ae0f05c21d39d338d09f58d0d2d
3
+ metadata.gz: '0841cc3eab093abcc6a954e3d24c1153de824d02'
4
+ data.tar.gz: 46f0b23b29b62843bbefbe73615ac318c1082214
5
5
  SHA512:
6
- metadata.gz: 51a6fe55e765bd7d78114cc8d3d77aa7eaac165105292e79e57f64f6d80367b7de3173e25591244b4aa9412d0c38ffc88e24926446a2865d037f4efff2bc6f9e
7
- data.tar.gz: 6fa5f5a13a1834194fda4cbef8701c80be10817c648d3f97c1d6a5d51417f5b35e247a3d2f6f9a5c54ee1d59085574302976ef19049e75418fa20a9c8572046b
6
+ metadata.gz: dc93171e985aa842a237ddce20c0ab935d5bcb776a63153e3ca6429018435e8cae0dff833641ea6c39b6c6184d838df00d26610707b80acd731ec762cbe20015
7
+ data.tar.gz: eb66c3a879ef980ab1679a8e6b5150cc9c0680ac85d6664a1f5d255f40e92fa9a051d4771116a9bc956f143c1459c1657c28e478c36125c6356ed3a78486d143
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
+ --format doc
2
3
  --require spec_helper
data/README.md CHANGED
@@ -90,5 +90,9 @@ pipeline.add pipe2
90
90
  ```
91
91
  This will override the timeout value set by the class definition.
92
92
 
93
+ ## Related Projects
94
+
95
+ * [PiecePipe](https://github.com/atomicobject/piece_pipe), an alternative implementation of the pipeline pattern.
96
+
93
97
  ## License
94
98
  **SimplePipeline** is released under the [MIT license](MIT-LICENSE).
@@ -17,12 +17,6 @@ class SimplePipeline
17
17
  @timeout_in_sec = sec
18
18
  end
19
19
 
20
- def process_with_timeout (payload)
21
- ::Timeout::timeout(timeout) {
22
- process(payload)
23
- }
24
- end
25
-
26
20
  def timeout
27
21
  @timeout_in_sec || self.class.class_variable_get(:@@timeout_in_sec)
28
22
  end
@@ -1,3 +1,3 @@
1
- class SimplePipeline
2
- VERSION = "0.0.3"
3
- end
1
+ class SimplePipeline
2
+ VERSION = "0.0.4"
3
+ end
@@ -1,35 +1,78 @@
1
- require "simple_pipeline/timeout"
2
- require "simple_pipeline/version"
3
-
4
-
5
- class SimplePipeline
6
- def initialize
7
- @pipes = []
8
- end
9
-
10
- def add (pipe)
11
- begin
12
- raise ArgumentError, "invalid pipe - incorrect number of arguments for process() method (should be 1)" unless pipe.class.instance_method(:process).arity == 1
13
- rescue NameError
14
- raise ArgumentError, "invalid pipe - process() method not found"
15
- end
16
-
17
- @pipes << pipe
18
- end
19
-
20
- def size
21
- return @pipes.size
22
- end
23
-
24
- def process (payload)
25
- @pipes.each do |pipe|
26
- if pipe.is_a? SimplePipeline::Timeout
27
- pipe.process_with_timeout(payload)
28
- else
29
- pipe.process(payload)
30
- end
31
- end
32
- end
33
-
34
- alias :length :size
1
+ require "simple_pipeline/timeout"
2
+ require "simple_pipeline/version"
3
+
4
+
5
+ class SimplePipeline
6
+
7
+ attr_reader :errors
8
+
9
+ def initialize
10
+ @pipe_order = []
11
+ @pipe_config = {}
12
+ @errors = []
13
+ end
14
+
15
+ def add (pipe, args = {})
16
+ begin
17
+ raise ArgumentError, "invalid pipe - incorrect number of arguments for process() method (should be 1)" unless pipe.class.instance_method(:process).arity == 1
18
+ rescue NameError
19
+ raise ArgumentError, "invalid pipe - process() method not found"
20
+ end
21
+
22
+ @pipe_order << pipe
23
+ @pipe_config[pipe] = args
24
+
25
+ return pipe
26
+ end
27
+
28
+ def size
29
+ return @pipe_order.size
30
+ end
31
+
32
+ def process (payload)
33
+ @pipe_order.each do |pipe|
34
+ 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
46
+ rescue
47
+ raise $! unless continue_on_error?($!, @pipe_config[pipe][:continue_on_error?])
48
+ @errors << $!
49
+ end
50
+ end
51
+ end
52
+
53
+ alias :length :size
54
+
55
+ private
56
+
57
+ def continue_on_error? (e, config_value)
58
+ return false if config_value.nil? || config_value == false
59
+ return true if config_value == true
60
+
61
+ if (config_value.is_a? Class) && (e.is_a? config_value)
62
+ return true
63
+ end
64
+
65
+ if config_value.is_a? Array
66
+ config_value.each { |klass| return true if (klass.is_a? Class) && (e.is_a? klass) }
67
+ end
68
+
69
+ return false
70
+ end
71
+
72
+
73
+ def process_with_timeout (pipe, payload, timeout)
74
+ ::Timeout::timeout(timeout) {
75
+ pipe.process(payload)
76
+ }
77
+ end
35
78
  end
@@ -22,6 +22,18 @@ class TimeoutPipe
22
22
  end
23
23
  end
24
24
 
25
+ class TimeoutPipe2
26
+ def process (payload)
27
+ sleep 10 # seconds
28
+ end
29
+ end
30
+
31
+ class ExceptionPipe
32
+ def process (payload)
33
+ raise ArgumentError.new "Something bad happened"
34
+ end
35
+ end
36
+
25
37
  describe SimplePipeline do
26
38
  it "should support three normal pipes" do
27
39
  pipeline = SimplePipeline.new
@@ -49,7 +61,7 @@ describe SimplePipeline do
49
61
  }.to raise_error(ArgumentError)
50
62
  end
51
63
 
52
- it "should support pipes with timeout" do
64
+ it "should support pipes with timeout values" do
53
65
  pipeline = SimplePipeline.new
54
66
  pipe = TimeoutPipe.new
55
67
  pipeline.add pipe
@@ -61,5 +73,44 @@ describe SimplePipeline do
61
73
  expect {
62
74
  pipeline.process({})
63
75
  }.to raise_error(Timeout::Error)
76
+
77
+ pipeline = SimplePipeline.new
78
+ pipeline.add TimeoutPipe2.new, :timeout => 1
79
+
80
+ expect {
81
+ pipeline.process({})
82
+ }.to raise_error(Timeout::Error)
83
+ end
84
+
85
+ it "should support pipes with continue_on_error => true" do
86
+ pipeline = SimplePipeline.new
87
+ pipeline.add TestPipe.new
88
+ pipeline.add ExceptionPipe.new
89
+ pipeline.add TestPipe.new
90
+
91
+ payload = {:test_value => 10}
92
+
93
+ expect {
94
+ pipeline.process(payload)
95
+ }.to raise_error(ArgumentError)
96
+
97
+ expect(payload[:test_value]).to eq 100
98
+ expect(pipeline.errors.size).to eq 0
99
+
100
+ pipeline = SimplePipeline.new
101
+ pipeline.add TestPipe.new
102
+ pipeline.add ExceptionPipe.new, :continue_on_error? => true
103
+ pipeline.add ExceptionPipe.new, :continue_on_error? => ArgumentError
104
+ pipeline.add ExceptionPipe.new, :continue_on_error? => StandardError
105
+ pipeline.add ExceptionPipe.new, :continue_on_error? => [ArgumentError]
106
+ pipeline.add ExceptionPipe.new, :continue_on_error? => [RuntimeError, StandardError]
107
+ pipeline.add TestPipe.new
108
+
109
+ payload = {:test_value => 10}
110
+
111
+ pipeline.process(payload)
112
+
113
+ expect(payload[:test_value]).to eq 1000
114
+ expect(pipeline.errors.size).to eq 5
64
115
  end
65
116
  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.3
4
+ version: 0.0.4
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-03 00:00:00.000000000 Z
11
+ date: 2016-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec