stages 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,10 +8,15 @@ Initial code stolen shamelessly from http://pragdave.blogs.pragprog.com/pragdave
8
8
  Usage
9
9
  -----
10
10
 
11
- You knit your stages together with '|'. The leftmost pipeline stage will be contain a generator, which usually is an infinite loop. For ean example, look at evens.rb in our sample stages collection. If you wanted to output, for example, every even number divisible by 3, you might do:
11
+ You knit your stages together with '|'. The leftmost pipeline stage will contain a generator, which usually is an infinite loop.
12
+
13
+ Example
14
+ -------
15
+
16
+ Image you wanted to output every even number divisible by 3. First you would need a class that would output even numbers. Then you would want to select the ones that are divisible by 3. A simple implementation using stages would like this
12
17
 
13
18
  ```ruby
14
- pipeline = Evens.new | MultiplesOf.new(3)
19
+ pipeline = Evens.new | Select.new{ |x| x % 3 == 0}
15
20
  loop { puts pipeline.run }
16
21
  ```
17
22
 
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler'
3
3
  begin
4
- Bundler.setup(:default, :development)
4
+ Bundler.setup(:default, :test)
5
5
  rescue Bundler::BundlerError => e
6
6
  $stderr.puts e.message
7
7
  $stderr.puts "Run `bundle install` to install missing gems"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -38,11 +38,12 @@ def setup_pipeline
38
38
 
39
39
  each_character = Each.new{ |x| x.chars }
40
40
  trim_whitespace = Select.new{ |x| x != ' '}
41
- letters_in_each_line = SubStage.new(get_lyric | each_character | trim_whitespace)
41
+ letters_in_each_line = Wrap.new(get_lyric | each_character | trim_whitespace)
42
+ each_letter = Each.new
42
43
  each_note = Generator.new(@lyrics.keys)
43
44
  count_everything = Count.new
44
45
 
45
- each_note | letters_in_each_line | count_everything
46
+ each_note | letters_in_each_line | each_letter | count_everything
46
47
  end
47
48
 
48
49
  puts setup_pipeline.run.inspect
@@ -18,14 +18,24 @@ def setup_pipeline
18
18
 
19
19
  each_character = Each.new{ |x| x.chars }
20
20
  trim_whitespace = Select.new{ |x| x != ' '}
21
- letters_in_each_line = SubStage.new(get_lyric | each_character | trim_whitespace)
21
+ letters_in_each_line = Wrap.new(get_lyric | each_character | trim_whitespace)
22
22
  each_note = Each.new @lyrics.keys
23
23
  count_everything = Count.new
24
+ each_letter = Each.new
24
25
 
25
- each_note | letters_in_each_line | count_everything
26
+
27
+ each_note | letters_in_each_line | each_letter | count_everything
26
28
  end
27
29
 
28
30
  puts setup_pipeline.run.inspect
29
31
 
32
+ puts "one line at a time"
33
+ lyrics = Each.new(@lyrics.keys)
34
+ letters_in_chunks = Wrap.new(Map.new{ |x| @lyrics[x]} | Each.new{ |x| x.chars}).with_hash
35
+ each_letter = Each.new{ |x| x.values.first }
36
+ count = Count.new
37
+
38
+ p2 = lyrics | letters_in_chunks | each_letter | count
30
39
 
40
+ puts p2.run.inspect
31
41
 
@@ -0,0 +1,34 @@
1
+ module Stages
2
+ class Wrap < Stage
3
+ def initialize(pipeline)
4
+ @pipeline = pipeline
5
+ @cache = []
6
+ @output_style = :hash
7
+ super()
8
+ end
9
+
10
+ def array
11
+ @output_style = :array
12
+ self
13
+ end
14
+
15
+ def each
16
+ @output_style = :each
17
+ self
18
+ end
19
+
20
+ def process
21
+ while value = input
22
+ subpipe = Emit.new(value) | @pipeline
23
+ results = []
24
+ while v = subpipe.run
25
+ @output_style == :each ? output(v) : results << v
26
+ end
27
+ output results if @output_style == :array
28
+ output({ value => results}) if @output_style == :hash
29
+ @pipeline.drop_leftmost!
30
+ @pipeline.continue
31
+ end
32
+ end
33
+ end
34
+ end
data/stages.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "stages"
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["The Justice Eight"]
12
- s.date = "2012-01-14"
12
+ s.date = "2012-01-16"
13
13
  s.description = "pipeline builder"
14
14
  s.email = "support@igodigital.com"
15
15
  s.extra_rdoc_files = [
@@ -30,13 +30,12 @@ Gem::Specification.new do |s|
30
30
  "lib/stages/count.rb",
31
31
  "lib/stages/each.rb",
32
32
  "lib/stages/emit.rb",
33
- "lib/stages/evens.rb",
34
33
  "lib/stages/map.rb",
35
34
  "lib/stages/restrict.rb",
36
35
  "lib/stages/resume.rb",
37
36
  "lib/stages/resume_count.rb",
38
37
  "lib/stages/select.rb",
39
- "lib/stages/sub_stage.rb",
38
+ "lib/stages/wrap.rb",
40
39
  "stages.gemspec",
41
40
  "test/helper.rb",
42
41
  "test/test_pipeline.rb",
data/test/helper.rb CHANGED
@@ -5,6 +5,18 @@ require 'stages'
5
5
  require 'turn'
6
6
  require 'minitest/unit'
7
7
 
8
+ module Stages
9
+ class Evens < Stage
10
+ def process
11
+ value = 0
12
+ loop do
13
+ output(value)
14
+ value += 2
15
+ end
16
+ end
17
+ end
18
+ end
19
+
8
20
  class MiniTest::Unit::TestCase
9
21
  def self.test(name, &block)
10
22
  define_method 'test_' + name, &block
@@ -42,6 +42,20 @@ class TestPipeline < MiniTest::Unit::TestCase
42
42
  result << v
43
43
  end
44
44
  assert_equal([1, 2], result)
45
- end
45
+ end
46
+
47
+ test 'complex substage hash example' do
48
+ sub = Each.new{ |x| x.chars } | Map.new{ |x| x.to_sym} | Count.new
49
+ pipeline = Each.new(%w(foo bar)) | Wrap.new(sub) | Map.new{ |x| { x.keys.first => x.values.first.first}}
50
+ result = pipeline.run
51
+ assert_equal({'foo' => { :f => 1, :o => 2}}, result)
52
+ result = pipeline.run
53
+ assert_equal({ 'bar' => { :b => 1, :a => 1, :r => 1}}, result)
54
+ end
55
+
56
+ test 'reset! resets everything' do
57
+ assert false
58
+ #todo
59
+ end
46
60
 
47
61
  end
data/test/test_stages.rb CHANGED
@@ -69,22 +69,28 @@ class TestStages < MiniTest::Unit::TestCase
69
69
  assert_equal({ 'foo' => { :f => 1, :o => 2}}, result)
70
70
  end
71
71
 
72
- test 'substage instead of resume' do
73
- sub = Each.new{ |x| x.chars } | Map.new{ |x| x.to_sym} | Count.new
74
- pipeline = Each.new(%w(foo bar)) | SubStage.new(sub).with_hash | Map.new{ |x| x.values.first}
72
+ test 'hash mode wrap' do
73
+ pipeline = Each.new(%w(foo bar)) | Wrap.new(Each.new{ |x| x.chars})
75
74
  result = pipeline.run
76
- assert_equal({ :f => 1, :o => 2}, result)
75
+ assert_equal(%w(f o o), result['foo'])
77
76
  result = pipeline.run
78
- assert_equal({ :b => 1, :a => 1, :r => 1}, result)
77
+ assert_equal(%w(b a r), result['bar'])
79
78
  end
80
79
 
81
- test 'substage with key and result' do
82
- sub = Each.new{ |x| x.chars } | Map.new{ |x| x.to_sym} | Count.new
83
- pipeline = Each.new(%w(foo bar)) | SubStage.new(sub).with_hash
80
+ test 'array mode wrap' do
81
+ pipeline = Each.new(%w(foo bar)) | Wrap.new(Each.new{ |x| x.chars}).array
84
82
  result = pipeline.run
85
- assert_equal({'foo' => { :f => 1, :o => 2}}, result)
83
+ assert_equal(%w(f o o), result)
86
84
  result = pipeline.run
87
- assert_equal({ 'bar' => { :b => 1, :a => 1, :r => 1}}, result)
85
+ assert_equal(%w(b a r), result)
86
+ end
87
+
88
+ test 'each mode wrap' do
89
+ pipeline = Each.new(%w(foo bar)) | Wrap.new(Each.new{ |x| x.chars}).each
90
+ expected = %w(r a b o o f)
91
+ while r = pipeline.run
92
+ assert_equal(expected.pop, r)
93
+ end
88
94
  end
89
95
 
90
96
  def sing
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: stages
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - The Justice Eight
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-01-14 00:00:00 Z
13
+ date: 2012-01-16 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -46,13 +46,12 @@ files:
46
46
  - lib/stages/count.rb
47
47
  - lib/stages/each.rb
48
48
  - lib/stages/emit.rb
49
- - lib/stages/evens.rb
50
49
  - lib/stages/map.rb
51
50
  - lib/stages/restrict.rb
52
51
  - lib/stages/resume.rb
53
52
  - lib/stages/resume_count.rb
54
53
  - lib/stages/select.rb
55
- - lib/stages/sub_stage.rb
54
+ - lib/stages/wrap.rb
56
55
  - stages.gemspec
57
56
  - test/helper.rb
58
57
  - test/test_pipeline.rb
@@ -70,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
69
  requirements:
71
70
  - - ">="
72
71
  - !ruby/object:Gem::Version
73
- hash: 1777887051892301360
72
+ hash: 1341798131382787657
74
73
  segments:
75
74
  - 0
76
75
  version: "0"
data/lib/stages/evens.rb DELETED
@@ -1,11 +0,0 @@
1
- module Stages
2
- class Evens < Stage
3
- def process
4
- value = 0
5
- loop do
6
- output(value)
7
- value += 2
8
- end
9
- end
10
- end
11
- end
@@ -1,27 +0,0 @@
1
- module Stages
2
- class SubStage < Stage
3
- def initialize(pipeline)
4
- @pipeline = pipeline
5
- @cache = []
6
- @with_hash = false
7
- super()
8
- end
9
-
10
- def with_hash
11
- @with_hash = true
12
- self
13
- end
14
-
15
- def process
16
- while value = input
17
- subpipe = Emit.new(value) | @pipeline
18
- while v = subpipe.run
19
- v = { value => v} if @with_hash
20
- output v
21
- end
22
- @pipeline.drop_leftmost!
23
- @pipeline.continue
24
- end
25
- end
26
- end
27
- end