simple_pipeline 0.0.2 → 0.0.3

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: d7498222868fb61586a42e233b459aed023f91a4
4
- data.tar.gz: 157c751b613f41bd2b0a507606a4ed2be6b5538b
3
+ metadata.gz: c4919357b1935895facfb4607f74def1253c1dea
4
+ data.tar.gz: 5dace42d21950ae0f05c21d39d338d09f58d0d2d
5
5
  SHA512:
6
- metadata.gz: e86424d591224a4bd4e0822666c83877f0fa497d33c570989748329c007842e8d7c3f7d7861262a75a1b01252014f838bc2d9bee772a459a2d22107b13b9b689
7
- data.tar.gz: 2d6b0b662fd764343825ca652469940879e270434d4b0a7f8185aa8d74e988bd8f60dfbf73bf57fe4a620c937e0d8e22183629adae1676c2faba1448053c37c4
6
+ metadata.gz: 51a6fe55e765bd7d78114cc8d3d77aa7eaac165105292e79e57f64f6d80367b7de3173e25591244b4aa9412d0c38ffc88e24926446a2865d037f4efff2bc6f9e
7
+ data.tar.gz: 6fa5f5a13a1834194fda4cbef8701c80be10817c648d3f97c1d6a5d51417f5b35e247a3d2f6f9a5c54ee1d59085574302976ef19049e75418fa20a9c8572046b
data/README.md CHANGED
@@ -1,31 +1,94 @@
1
- # SimplePipeline
2
-
3
- TODO: Write a gem description
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- ```ruby
10
- gem 'simple_pipeline'
11
- ```
12
-
13
- And then execute:
14
-
15
- $ bundle
16
-
17
- Or install it yourself as:
18
-
19
- $ gem install simple_pipeline
20
-
21
- ## Usage
22
-
23
- TODO: Write usage instructions here
24
-
25
- ## Contributing
26
-
27
- 1. Fork it ( https://github.com/[my-github-username]/simple_pipeline/fork )
28
- 2. Create your feature branch (`git checkout -b my-new-feature`)
29
- 3. Commit your changes (`git commit -am 'Add some feature'`)
30
- 4. Push to the branch (`git push origin my-new-feature`)
31
- 5. Create a new Pull Request
1
+ # SimplePipeline
2
+
3
+ **SimplePipeline** is a simple framework for sequentially executing a series of code components. Each code component takes a payload, modifies it, and moves it down the pipe for the next component and so forth. One of the design goals is to make it as flexible as possible, yet keep it simple.
4
+
5
+ ## Installation
6
+
7
+ Install the gem from RubyGems:
8
+
9
+ ```bash
10
+ gem install simple_pipeline
11
+ ```
12
+
13
+ If you use Bundler, just add it to your Gemfile and run `bundle install`
14
+
15
+ ```ruby
16
+ gem 'simple_pipeline'
17
+ ```
18
+
19
+ I have only tested this gem on Ruby 2.3.0, but there shouldn't be any reason why it wouldn't work on earlier Ruby versions as well.
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ pipeline = SimplePipeline.new
25
+
26
+ pipeline.add Pipe1.new
27
+ pipeline.add Pipe2.new
28
+ pipeline.add Pipe3.new
29
+
30
+ payload = {:some_key => some_value}
31
+
32
+ pipeline.process payload
33
+ ```
34
+
35
+ **SimplePipeline** will call the ```process``` method on each of the pipes in the order that they were added to the pipeline.
36
+
37
+ ## Pipes
38
+
39
+ A **pipe** can be any Ruby object that has a ```process``` method that accepts a single argument (the payload object). For example:
40
+
41
+ ```ruby
42
+ class Pipe1
43
+ def process (payload)
44
+ # Do something with the payload object
45
+ end
46
+ end
47
+ ```
48
+
49
+ ## Payload
50
+
51
+ The **payload** can be an Array, Hash, or any other Ruby object. Individual pipes have the responsibility to know what to do with the payload that is passed into the ```process``` method.
52
+
53
+ ## Timeout
54
+
55
+ You can use the **SimplePipeline::Timeout** mixin to enforce a timeout value (in seconds) for a pipe. If the execution of the ```process``` method exceeds the specified timeout value, a ```Timeout::Error``` will be thrown.
56
+
57
+ ```ruby
58
+ class TimeoutPipe
59
+ include SimplePipeline::Timeout
60
+
61
+ set_timeout 3 # Set the timeout value to be three seconds
62
+
63
+ def process (payload)
64
+ # Do something
65
+ end
66
+ end
67
+
68
+ pipeline = SimplePipeline.new
69
+ pipeline.add TimeoutPipe.new
70
+
71
+ payload = {:some_key => some_value}
72
+
73
+ # Will throw a Timeout::Error if execution of process on the TimeoutPipe instance takes longer than 3 seconds
74
+ pipeline.process payload
75
+ ```
76
+
77
+ You can also set the timeout value on a per instance basis.
78
+
79
+ ```ruby
80
+ pipeline = SimplePipeline.new
81
+
82
+ pipe1 = TimeoutPipe.new
83
+ pipe1.set_timeout 10
84
+
85
+ pipe2 = TimeoutPipe.new
86
+ pipe2.set_timeout 60
87
+
88
+ pipeline.add pipe1
89
+ pipeline.add pipe2
90
+ ```
91
+ This will override the timeout value set by the class definition.
92
+
93
+ ## License
94
+ **SimplePipeline** is released under the [MIT license](MIT-LICENSE).
@@ -3,35 +3,33 @@ require "simple_pipeline/version"
3
3
 
4
4
 
5
5
  class SimplePipeline
6
-
7
- def initialize
8
- @pipes = []
9
- end
10
-
11
- def add (pipe)
12
- begin
13
- raise ArgumentError, "invalid pipe - incorrect number of arguments for process() method (should be 1)" unless pipe.class.instance_method(:process).arity == 1
14
- rescue NameError
15
- raise ArgumentError, "invalid pipe - process() method not found"
16
- end
17
-
18
- @pipes << pipe
19
- end
20
-
21
- def size
22
- return @pipes.size
23
- end
24
-
25
- def process (payload)
26
- @pipes.each do |pipe|
27
- if pipe.is_a? SimplePipeline::Timeout
28
- pipe.process_with_timeout(payload)
29
- else
30
- pipe.process(payload)
31
- end
32
- end
33
- end
34
-
35
- alias :length :size
36
-
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
37
35
  end
@@ -1,19 +1,30 @@
1
1
  require 'timeout'
2
2
 
3
3
  class SimplePipeline
4
+ module Timeout
5
+ def self.included (base)
6
+ base.extend(ClassMethods)
7
+ base.class_variable_set(:@@timeout_in_sec, 0)
8
+ end
4
9
 
5
- module Timeout
10
+ module ClassMethods
11
+ def set_timeout (sec)
12
+ self.class_variable_set(:@@timeout_in_sec, sec)
13
+ end
14
+ end
6
15
 
7
- def set_timeout (sec)
8
- @timeout_in_sec = sec
9
- end
16
+ def timeout=(sec)
17
+ @timeout_in_sec = sec
18
+ end
10
19
 
11
- def process_with_timeout (payload)
12
- ::Timeout::timeout(@timeout_in_sec) {
13
- process(payload)
14
- }
15
- end
16
-
17
- end
20
+ def process_with_timeout (payload)
21
+ ::Timeout::timeout(timeout) {
22
+ process(payload)
23
+ }
24
+ end
18
25
 
26
+ def timeout
27
+ @timeout_in_sec || self.class.class_variable_get(:@@timeout_in_sec)
28
+ end
29
+ end
19
30
  end
@@ -1,3 +1,3 @@
1
1
  class SimplePipeline
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,63 +1,65 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class TestPipe
4
- def process (payload)
5
- payload[:test_value] *= 10
6
- end
4
+ def process (payload)
5
+ payload[:test_value] *= 10
6
+ end
7
7
  end
8
8
 
9
9
  class BadPipe
10
- def process (payload1, payload2)
11
- # do nothing
12
- end
10
+ def process (payload1, payload2)
11
+ # do nothing
12
+ end
13
13
  end
14
14
 
15
15
  class TimeoutPipe
16
- include SimplePipeline::Timeout
16
+ include SimplePipeline::Timeout
17
17
 
18
- def initialize
19
- set_timeout 1 # seconds
20
- end
18
+ set_timeout 3 # seconds
21
19
 
22
- def process (payload)
23
- sleep 10 # seconds
24
- end
20
+ def process (payload)
21
+ sleep 10 # seconds
22
+ end
25
23
  end
26
24
 
27
25
  describe SimplePipeline do
28
- it "should support three normal pipes" do
29
- pipeline = SimplePipeline.new
30
- pipeline.add TestPipe.new
31
- pipeline.add TestPipe.new
32
- pipeline.add TestPipe.new
33
-
34
- payload = {:test_value => 10}
35
-
36
- pipeline.process(payload)
37
-
38
- expect(pipeline.size).to eq 3
39
- expect(payload[:test_value]).to eq 10000
40
- end
41
-
42
- it "should throw an error for invalid pipes" do
43
- pipeline = SimplePipeline.new
44
-
45
- expect {
46
- pipeline.add Object.new # not a pipe
47
- }.to raise_error(ArgumentError)
48
-
49
- expect {
50
- pipeline.add BadPipe
51
- }.to raise_error(ArgumentError)
52
- end
53
-
54
- it "should support pipes with timeout" do
55
- pipeline = SimplePipeline.new
56
- pipe = TimeoutPipe.new
57
- pipeline.add pipe
58
-
59
- expect {
60
- pipeline.process({})
61
- }.to raise_error(Timeout::Error)
62
- end
26
+ it "should support three normal pipes" do
27
+ pipeline = SimplePipeline.new
28
+ pipeline.add TestPipe.new
29
+ pipeline.add TestPipe.new
30
+ pipeline.add TestPipe.new
31
+
32
+ payload = {:test_value => 10}
33
+
34
+ pipeline.process(payload)
35
+
36
+ expect(pipeline.size).to eq 3
37
+ expect(payload[:test_value]).to eq 10000
38
+ end
39
+
40
+ it "should throw an error for invalid pipes" do
41
+ pipeline = SimplePipeline.new
42
+
43
+ expect {
44
+ pipeline.add Object.new # not a pipe
45
+ }.to raise_error(ArgumentError)
46
+
47
+ expect {
48
+ pipeline.add BadPipe
49
+ }.to raise_error(ArgumentError)
50
+ end
51
+
52
+ it "should support pipes with timeout" do
53
+ pipeline = SimplePipeline.new
54
+ pipe = TimeoutPipe.new
55
+ pipeline.add pipe
56
+
57
+ expect(pipe.timeout).to eq 3
58
+ pipe.timeout = 1
59
+ expect(pipe.timeout).to eq 1
60
+
61
+ expect {
62
+ pipeline.process({})
63
+ }.to raise_error(Timeout::Error)
64
+ end
63
65
  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.2
4
+ version: 0.0.3
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-02 00:00:00.000000000 Z
11
+ date: 2016-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec