stages 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -14,14 +14,14 @@ def sing
14
14
  end
15
15
 
16
16
  def setup_pipeline
17
- generator = EachElement.new sing.keys
17
+ generator = Each.new sing.keys
18
18
  loop = Restrict.new
19
- get_lyric = HashLookup.new sing
20
- each_character = EachInput.new{ |x| x.chars }
19
+ get_lyric = Map.new{ |x| sing[x]}
20
+ each_character = Each.new{ |x| x.chars }
21
21
  whitespace = Select.new{ |x| x != ' '}
22
22
  pool = ResumeCount.new
23
23
  subtotals = Map.new { |x| x.values.first }
24
- iterator = EachInput.new
24
+ iterator = Each.new
25
25
  aggregator = SuperAggregator.new
26
26
 
27
27
  generator | loop | get_lyric | each_character | whitespace | pool | subtotals | iterator | aggregator
@@ -0,0 +1,51 @@
1
+ require "#{File.dirname(__FILE__)}/../lib/stages"
2
+
3
+ include Stages
4
+
5
+ #count the occurance of each letter in these song lyrics
6
+ @lyrics =
7
+ { :do => 'doe a deer a female deer',
8
+ :re => 'ray a drop of golden sun',
9
+ :mi => 'me a name I call myself',
10
+ :fa => 'far a long long way to run',
11
+ :so => 'a needle pulling thread',
12
+ :la => 'a note to follow so',
13
+ :ti => 'a drink with jam and bread'}
14
+
15
+ class Lyrics < Stage
16
+ attr_accessor :lyrics
17
+ def handle_value(value)
18
+ output @lyrics[value]
19
+ end
20
+ end
21
+
22
+ class Generator < Stage
23
+ def initialize(things)
24
+ @things = things
25
+ super()
26
+ end
27
+
28
+ def process
29
+ @things.each do |t|
30
+ output t
31
+ end
32
+ end
33
+ end
34
+
35
+ def setup_pipeline
36
+ get_lyric = Lyrics.new
37
+ get_lyric.lyrics = @lyrics
38
+
39
+ each_character = Each.new{ |x| x.chars }
40
+ trim_whitespace = Select.new{ |x| x != ' '}
41
+ letters_in_each_line = SubStage.new(get_lyric | each_character | trim_whitespace)
42
+ each_note = Generator.new(@lyrics.keys)
43
+ count_everything = Count.new
44
+
45
+ each_note | letters_in_each_line | count_everything
46
+ end
47
+
48
+ puts setup_pipeline.run.inspect
49
+
50
+
51
+
@@ -3,7 +3,7 @@ require "#{File.dirname(__FILE__)}/../lib/stages"
3
3
  include Stages
4
4
 
5
5
  #count the occurance of each letter in these song lyrics
6
- def sing
6
+ @lyrics =
7
7
  { :do => 'doe a deer a female deer',
8
8
  :re => 'ray a drop of golden sun',
9
9
  :mi => 'me a name I call myself',
@@ -11,18 +11,18 @@ def sing
11
11
  :so => 'a needle pulling thread',
12
12
  :la => 'a note to follow so',
13
13
  :ti => 'a drink with jam and bread'}
14
- end
15
14
 
16
15
  def setup_pipeline
17
- get_lyric = HashLookup.new sing
18
- each_character = EachInput.new{ |x| x.chars }
19
- trim_whitespace = Select.new{ |x| x != ' '}
20
- count_letters_in_line = SubStage.new(get_lyric | each_character | trim_whitespace)
16
+ get_lyric = Map.new{ |x| @lyrics[x]}
21
17
 
22
- each_note = EachElement.new sing.keys
18
+
19
+ each_character = Each.new{ |x| x.chars }
20
+ trim_whitespace = Select.new{ |x| x != ' '}
21
+ letters_in_each_line = SubStage.new(get_lyric | each_character | trim_whitespace)
22
+ each_note = Each.new @lyrics.keys
23
23
  count_everything = Count.new
24
24
 
25
- each_note | count_letters_in_line | count_everything
25
+ each_note | letters_in_each_line | count_everything
26
26
  end
27
27
 
28
28
  puts setup_pipeline.run.inspect
@@ -0,0 +1,34 @@
1
+ module Stages
2
+ class Each < Stage
3
+ def initialize(things = nil, &block)
4
+ @things = things unless things.nil?
5
+ @block = block
6
+ super()
7
+ end
8
+
9
+ def process
10
+ if @things
11
+ process_things
12
+ else
13
+ process_inputs
14
+ end
15
+ end
16
+
17
+ def process_inputs
18
+ while v = input
19
+ v = @block.call(v) if @block
20
+ v.each do |v|
21
+ output v
22
+ end
23
+ end
24
+ end
25
+
26
+ def process_things
27
+ @things = @block.call(@things) if @block
28
+ output nil if @things.nil?
29
+ @things.each do |thing|
30
+ output thing
31
+ end
32
+ end
33
+ end
34
+ end
@@ -3,13 +3,20 @@ module Stages
3
3
  def initialize(pipeline)
4
4
  @pipeline = pipeline
5
5
  @cache = []
6
+ @with_hash = false
6
7
  super()
7
8
  end
8
9
 
10
+ def with_hash
11
+ @with_hash = true
12
+ self
13
+ end
14
+
9
15
  def process
10
16
  while value = input
11
17
  subpipe = Emit.new(value) | @pipeline
12
18
  while v = subpipe.run
19
+ v = { value => v} if @with_hash
13
20
  output v
14
21
  end
15
22
  @pipeline.drop_leftmost!
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "stages"
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
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"]
@@ -23,17 +23,15 @@ Gem::Specification.new do |s|
23
23
  "Rakefile",
24
24
  "VERSION",
25
25
  "examples/sing.rb",
26
+ "examples/sing_custom_stages.rb",
26
27
  "examples/sing_subpipes.rb",
27
28
  "lib/stage_base.rb",
28
29
  "lib/stages.rb",
29
30
  "lib/stages/count.rb",
30
- "lib/stages/each_element.rb",
31
- "lib/stages/each_input.rb",
31
+ "lib/stages/each.rb",
32
32
  "lib/stages/emit.rb",
33
33
  "lib/stages/evens.rb",
34
- "lib/stages/hash_lookup.rb",
35
34
  "lib/stages/map.rb",
36
- "lib/stages/multiples_of.rb",
37
35
  "lib/stages/restrict.rb",
38
36
  "lib/stages/resume.rb",
39
37
  "lib/stages/resume_count.rb",
@@ -5,8 +5,8 @@ class TestPipeline < MiniTest::Unit::TestCase
5
5
 
6
6
  test 'simple basic pipeline' do
7
7
  evens = Evens.new
8
- mx3 = MultiplesOf.new(3)
9
- mx7 = MultiplesOf.new(7)
8
+ mx3 = Select.new{ |x| x % 3 == 0}
9
+ mx7 = Select.new{ |x| x % 7 == 0}
10
10
  mx3.source = evens
11
11
  mx7.source = mx3
12
12
 
@@ -15,7 +15,7 @@ class TestPipeline < MiniTest::Unit::TestCase
15
15
  end
16
16
 
17
17
  test 'pipeline pipe syntax works' do
18
- pipeline = Evens.new | MultiplesOf.new(3) | MultiplesOf.new(7)
18
+ pipeline = Evens.new | Select.new{ |x| x % 3 == 0} | Select.new{ |x| x % 7 == 0}
19
19
  result = (0..2).map{ pipeline.run }
20
20
  assert_equal([0, 42, 84], result)
21
21
  end
@@ -36,7 +36,7 @@ class TestPipeline < MiniTest::Unit::TestCase
36
36
  end
37
37
 
38
38
  test 'nil kills it' do
39
- pipeline = EachElement.new([1, 2, nil, 3])
39
+ pipeline = Each.new([1, 2, nil, 3])
40
40
  result = []
41
41
  while v = pipeline.run
42
42
  result << v
@@ -22,19 +22,19 @@ class TestStages < MiniTest::Unit::TestCase
22
22
  end
23
23
 
24
24
  test 'multiples_of' do
25
- pipeline = Evens.new | MultiplesOf.new(3)
25
+ pipeline = Evens.new | Select.new{ |x| x % 3 == 0}
26
26
  result = (0..3).map{ pipeline.run }
27
27
  assert_equal([0, 6, 12, 18], result)
28
28
  end
29
29
 
30
30
  test 'each_element' do
31
- pipeline = EachElement.new([1, 2, 3])
31
+ pipeline = Each.new([1, 2, 3])
32
32
  result = (0..2).map{ pipeline.run }
33
33
  assert_equal([1, 2, 3], result)
34
34
  end
35
35
 
36
36
  test 'hash_lookup' do
37
- pipeline = EachElement.new([:do, :re, :mi]) | HashLookup.new(sing)
37
+ pipeline = Each.new([:do, :re, :mi]) | Map.new{ |x| sing[x]}
38
38
  result = (0..2).map { pipeline.run }
39
39
  assert_equal(['doe a deer a female deer', 'ray a drop of golden sun', 'me a name I call myself'], result)
40
40
  end
@@ -53,17 +53,8 @@ class TestStages < MiniTest::Unit::TestCase
53
53
  assert_equal([0, 4], result)
54
54
  end
55
55
 
56
- test 'each_input' do
57
- pipeline = EachElement.new([[1, 2], [3, 4]]) | EachInput.new
58
- result = []
59
- while v = pipeline.run
60
- result << v
61
- end
62
- assert_equal([1, 2, 3, 4], result)
63
- end
64
-
65
56
  test 'resume' do
66
- pipeline = EachElement.new(%w(foo bar)) | Restrict.new | EachInput.new{ |x| x.chars} | Resume.new
57
+ pipeline = Each.new(%w(foo bar)) | Restrict.new | Each.new{ |x| x.chars} | Resume.new
67
58
  result = []
68
59
  while v = pipeline.run
69
60
  result << v
@@ -73,20 +64,29 @@ class TestStages < MiniTest::Unit::TestCase
73
64
 
74
65
  test 'resume with count' do
75
66
  resume = ResumeCount.new
76
- pipeline = EachElement.new(%w(foo bar)) | Restrict.new | EachInput.new{ |x| x.chars} | Map.new{ |x| x.to_sym } | resume
67
+ pipeline = Each.new(%w(foo bar)) | Restrict.new | Each.new{ |x| x.chars} | Map.new{ |x| x.to_sym } | resume
77
68
  result = pipeline.run
78
69
  assert_equal({ 'foo' => { :f => 1, :o => 2}}, result)
79
70
  end
80
71
 
81
72
  test 'substage instead of resume' do
82
- sub = EachInput.new{ |x| x.chars } | Map.new{ |x| x.to_sym} | Count.new
83
- pipeline = EachElement.new(%w(foo bar)) | SubStage.new(sub)
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}
84
75
  result = pipeline.run
85
76
  assert_equal({ :f => 1, :o => 2}, result)
86
77
  result = pipeline.run
87
78
  assert_equal({ :b => 1, :a => 1, :r => 1}, result)
88
79
  end
89
80
 
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
84
+ result = pipeline.run
85
+ assert_equal({'foo' => { :f => 1, :o => 2}}, result)
86
+ result = pipeline.run
87
+ assert_equal({ 'bar' => { :b => 1, :a => 1, :r => 1}}, result)
88
+ end
89
+
90
90
  def sing
91
91
  { :do => 'doe a deer a female deer',
92
92
  :re => 'ray a drop of golden sun',
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: stages
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - The Justice Eight
@@ -39,17 +39,15 @@ files:
39
39
  - Rakefile
40
40
  - VERSION
41
41
  - examples/sing.rb
42
+ - examples/sing_custom_stages.rb
42
43
  - examples/sing_subpipes.rb
43
44
  - lib/stage_base.rb
44
45
  - lib/stages.rb
45
46
  - lib/stages/count.rb
46
- - lib/stages/each_element.rb
47
- - lib/stages/each_input.rb
47
+ - lib/stages/each.rb
48
48
  - lib/stages/emit.rb
49
49
  - lib/stages/evens.rb
50
- - lib/stages/hash_lookup.rb
51
50
  - lib/stages/map.rb
52
- - lib/stages/multiples_of.rb
53
51
  - lib/stages/restrict.rb
54
52
  - lib/stages/resume.rb
55
53
  - lib/stages/resume_count.rb
@@ -72,7 +70,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
70
  requirements:
73
71
  - - ">="
74
72
  - !ruby/object:Gem::Version
75
- hash: 4250500444924917482
73
+ hash: 1777887051892301360
76
74
  segments:
77
75
  - 0
78
76
  version: "0"
@@ -1,14 +0,0 @@
1
- module Stages
2
- class EachElement < Stage
3
- def initialize(things)
4
- @things = things
5
- super()
6
- end
7
-
8
- def process
9
- @things.each do |thing|
10
- output thing
11
- end
12
- end
13
- end
14
- end
@@ -1,10 +0,0 @@
1
- module Stages
2
- class EachInput < Stage
3
- def handle_value(value)
4
- value = @block.call(value) if @block
5
- value.each do |i|
6
- output i
7
- end
8
- end
9
- end
10
- end
@@ -1,12 +0,0 @@
1
- module Stages
2
- class HashLookup < Stage
3
- def initialize(things)
4
- @things = things
5
- super()
6
- end
7
-
8
- def handle_value(value)
9
- output @things[value]
10
- end
11
- end
12
- end
@@ -1,12 +0,0 @@
1
- module Stages
2
- class MultiplesOf < Stage
3
- def initialize(factor)
4
- @factor = factor
5
- super()
6
- end
7
-
8
- def handle_value(value)
9
- output(value) if value % @factor == 0
10
- end
11
- end
12
- end