stages 0.2.5 → 0.2.6

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.
data/.autotest CHANGED
@@ -4,7 +4,10 @@ Autotest.add_hook(:initialize) do |at|
4
4
  end
5
5
 
6
6
  at.add_mapping(%r%^lib(/stages)?/(.*).rb$%, true) do |filename, _|
7
- ['test/test_pipeline.rb', 'test/test_stages.rb']
7
+ ['test/test_pipeline.rb', 'test/test_stages.rb', 'test/test_syntax.rb']
8
8
  end
9
9
 
10
+ at.add_mapping(%r%^lib/sugar.rb$%, true) do |filename, _|
11
+ ['test/test_syntax.rb']
12
+ end
10
13
  end
data/README.md CHANGED
@@ -5,6 +5,8 @@ A gem for creating data pipelines out of tiny, reusable objects
5
5
 
6
6
  Initial code stolen shamelessly from http://pragdave.blogs.pragprog.com/pragdave/2008/01/pipelines-using.html
7
7
 
8
+ Announcement blog post is at http://blog.igodigital.com/blog/on-recommendations/the-stages-gem
9
+
8
10
  Usage
9
11
  -----
10
12
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.2.6
data/lib/stages.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require "#{File.dirname(__FILE__)}/stage_base"
2
2
  Dir["#{File.dirname(__FILE__)}/stages/*.rb"].each { |file| require file.gsub(".rb", "")}
3
+ require "#{File.dirname(__FILE__)}/sugar"
3
4
 
data/lib/sugar.rb ADDED
@@ -0,0 +1,39 @@
1
+ module Stages
2
+ module Sugar
3
+ def select(*args, &block)
4
+ Select.new(*args, &block)
5
+ end
6
+
7
+ def each(*args, &block)
8
+ Each.new(*args, &block)
9
+ end
10
+
11
+ def map(*args, &block)
12
+ Map.new(*args, &block)
13
+ end
14
+
15
+ def wrap(*args, &block)
16
+ Wrap.new(*args, &block)
17
+ end
18
+
19
+ def unique(*args, &block)
20
+ Unique.new(*args, &block)
21
+ end
22
+
23
+ def run_until_exhausted(*args, &block)
24
+ Exhaust.new(*args, &block)
25
+ end
26
+
27
+ def restrict(*args, &block)
28
+ Restrict.new(*args, &block)
29
+ end
30
+
31
+ def resume(*args, &block)
32
+ Resume.new(*args, &block)
33
+ end
34
+
35
+ def group(*args, &block)
36
+ Count.new
37
+ end
38
+ end
39
+ 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.2.5"
8
+ s.version = "0.2.6"
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-18"
12
+ s.date = "2012-01-21"
13
13
  s.description = "pipeline builder"
14
14
  s.email = "support@igodigital.com"
15
15
  s.extra_rdoc_files = [
@@ -38,10 +38,12 @@ Gem::Specification.new do |s|
38
38
  "lib/stages/select.rb",
39
39
  "lib/stages/unique.rb",
40
40
  "lib/stages/wrap.rb",
41
+ "lib/sugar.rb",
41
42
  "stages.gemspec",
42
43
  "test/helper.rb",
43
44
  "test/test_pipeline.rb",
44
- "test/test_stages.rb"
45
+ "test/test_stages.rb",
46
+ "test/test_syntax.rb"
45
47
  ]
46
48
  s.homepage = "https://github.com/iGoDigital-LLC/stages"
47
49
  s.require_paths = ["lib"]
data/test/helper.rb CHANGED
@@ -7,7 +7,7 @@ require 'minitest/unit'
7
7
 
8
8
  module Stages
9
9
  class Evens < Stage
10
- def process
10
+ def process
11
11
  value = 0
12
12
  loop do
13
13
  output(value)
@@ -17,6 +17,12 @@ module Stages
17
17
  end
18
18
  end
19
19
 
20
+ module Stages::Sugar
21
+ def evens
22
+ Evens.new
23
+ end
24
+ end
25
+
20
26
  class MiniTest::Unit::TestCase
21
27
  def self.test(name, &block)
22
28
  define_method 'test_' + name, &block
@@ -33,12 +39,12 @@ class MiniTest::Unit
33
39
  if test_cases.size > 0
34
40
  @@out.print "\n#{suite}:\n"
35
41
  end
36
-
42
+
37
43
  test_cases.each do |test|
38
44
  inst = suite.new test
39
45
  inst._assertions = 0
40
-
41
-
46
+
47
+
42
48
  @broken = nil
43
49
 
44
50
  @@out.print(case run_one inst
@@ -63,7 +69,7 @@ class MiniTest::Unit
63
69
  end)
64
70
 
65
71
  if @broken
66
- @@out.print MiniTest::Unit.use_natural_language_case_names? ?
72
+ @@out.print MiniTest::Unit.use_natural_language_case_names? ?
67
73
  " #{test.gsub("test_", "").gsub(/_/, " ")}" : " #{test}"
68
74
  @@out.print " (%.2fs) " % @elapsed
69
75
  @@out.puts
@@ -84,14 +90,14 @@ class MiniTest::Unit
84
90
  @@out.sync = old_sync if @@out.respond_to? :sync=
85
91
  [@test_count, @assertion_count]
86
92
  end
87
-
93
+
88
94
  def run_one(inst)
89
95
  t = Time.now
90
96
  result = inst.run self
91
97
  @elapsed = Time.now - t
92
98
  if result == :pass && @elapsed > 5.0
93
99
  result = :timeout
94
- @report << { :message => "Test took a long time (%.2fs)" % @elapsed,
100
+ @report << { :message => "Test took a long time (%.2fs)" % @elapsed,
95
101
  :exception => Exception.new("Long test")}
96
102
  end
97
103
  result
data/test/test_stages.rb CHANGED
@@ -2,43 +2,36 @@ require 'helper'
2
2
  include Stages
3
3
 
4
4
  class TestStages < MiniTest::Unit::TestCase
5
-
6
5
  test 'evens' do
7
6
  evens = Evens.new
8
7
  result = (0..2).map{ evens.run }
9
8
  assert_equal([0, 2, 4], result)
10
9
  end
11
-
10
+
12
11
  test 'select' do
13
12
  pipeline = Evens.new | Select.new{ |val| val > 6}
14
13
  result = (0..2).map{ pipeline.run }
15
14
  assert_equal([8, 10, 12], result)
16
15
  end
17
-
16
+
18
17
  test 'map' do
19
18
  pipeline = Evens.new | Map.new{ |val| val * 3}
20
19
  result = (0..2).map{ pipeline.run }
21
20
  assert_equal([0, 6, 12], result)
22
21
  end
23
-
24
- test 'multiples_of' do
25
- pipeline = Evens.new | Select.new{ |x| x % 3 == 0}
26
- result = (0..3).map{ pipeline.run }
27
- assert_equal([0, 6, 12, 18], result)
28
- end
29
-
22
+
30
23
  test 'each_element' do
31
24
  pipeline = Each.new([1, 2, 3])
32
25
  result = (0..2).map{ pipeline.run }
33
26
  assert_equal([1, 2, 3], result)
34
27
  end
35
-
28
+
36
29
  test 'hash_lookup' do
37
30
  pipeline = Each.new([:do, :re, :mi]) | Map.new{ |x| sing[x]}
38
31
  result = (0..2).map { pipeline.run }
39
32
  assert_equal(['doe a deer a female deer', 'ray a drop of golden sun', 'me a name I call myself'], result)
40
- end
41
-
33
+ end
34
+
42
35
  test 'restrict' do
43
36
  pipeline = Evens.new | Restrict.new | Map.new{ |x| x * 2}
44
37
  result = []
@@ -52,7 +45,7 @@ class TestStages < MiniTest::Unit::TestCase
52
45
  end
53
46
  assert_equal([0, 4], result)
54
47
  end
55
-
48
+
56
49
  test 'resume' do
57
50
  pipeline = Each.new(%w(foo bar)) | Restrict.new | Each.new{ |x| x.chars} | Resume.new
58
51
  result = []
@@ -61,14 +54,14 @@ class TestStages < MiniTest::Unit::TestCase
61
54
  end
62
55
  assert_equal([{ 'foo' => %w(f o o)}, {'bar' => %w(b a r)}], result)
63
56
  end
64
-
57
+
65
58
  test 'resume with count' do
66
59
  resume = ResumeCount.new
67
60
  pipeline = Each.new(%w(foo bar)) | Restrict.new | Each.new{ |x| x.chars} | Map.new{ |x| x.to_sym } | resume
68
61
  result = pipeline.run
69
62
  assert_equal({ 'foo' => { :f => 1, :o => 2}}, result)
70
63
  end
71
-
64
+
72
65
  test 'hash mode wrap' do
73
66
  pipeline = Each.new(%w(foo bar)) | Wrap.new(Each.new{ |x| x.chars})
74
67
  result = pipeline.run
@@ -76,7 +69,7 @@ class TestStages < MiniTest::Unit::TestCase
76
69
  result = pipeline.run
77
70
  assert_equal(%w(b a r), result['bar'])
78
71
  end
79
-
72
+
80
73
  test 'hash/aggregated wrap mode wrap' do
81
74
  pipeline = Each.new(%w(foo bar)) | Wrap.new(Each.new{ |x| x.chars} | Count.new, :aggregated)
82
75
  result = pipeline.run
@@ -84,8 +77,8 @@ class TestStages < MiniTest::Unit::TestCase
84
77
  result = pipeline.run
85
78
  assert_equal(3, result['bar'].keys.length)
86
79
  end
87
-
88
-
80
+
81
+
89
82
  test 'array mode wrap' do
90
83
  pipeline = Each.new(%w(foo bar)) | Wrap.new(Each.new{ |x| x.chars}, :array)
91
84
  result = pipeline.run
@@ -93,15 +86,15 @@ class TestStages < MiniTest::Unit::TestCase
93
86
  result = pipeline.run
94
87
  assert_equal(%w(b a r), result)
95
88
  end
96
-
89
+
97
90
  test 'each mode wrap' do
98
91
  pipeline = Each.new(%w(foo bar)) | Wrap.new(Each.new{ |x| x.chars}, :each)
99
92
  expected = %w(r a b o o f)
100
93
  while r = pipeline.run
101
94
  assert_equal(expected.pop, r)
102
- end
95
+ end
103
96
  end
104
-
97
+
105
98
  test 'unique' do
106
99
  pipeline = Each.new('abcadefbega'){ |x| x.chars} | Unique.new
107
100
  results = []
@@ -110,12 +103,12 @@ class TestStages < MiniTest::Unit::TestCase
110
103
  end
111
104
  assert_equal(%w(a b c d e f g), results)
112
105
  end
113
-
106
+
114
107
  test 'exhaust' do
115
108
  pipeline = Each.new(%w(foo bar zut)) | Exhaust.new
116
109
  assert_equal(%w(foo bar zut), pipeline.run)
117
110
  end
118
-
111
+
119
112
  def sing
120
113
  { :do => 'doe a deer a female deer',
121
114
  :re => 'ray a drop of golden sun',
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+ include Stages
3
+
4
+ class TestSyntax < MiniTest::Unit::TestCase
5
+ include Stages::Sugar
6
+
7
+ test 'select and map' do
8
+ pipeline = evens | select{ |val| val > 6} | map{ |val| val * 2}
9
+ assert_equal(16, pipeline.run)
10
+ end
11
+
12
+ test 'each and group' do
13
+ pipeline = each([1, 2, 3]) | map{ |x| [1]*x} | each | group
14
+ assert_equal({ 1 => 6}, pipeline.run)
15
+ end
16
+
17
+ test 'restrict and resume' do
18
+ pipeline = each(%w(foo bar)) | restrict | each{ |x| x.chars} | resume
19
+ result = []
20
+ while v = pipeline.run
21
+ result << v
22
+ end
23
+ assert_equal([{ 'foo' => %w(f o o)}, {'bar' => %w(b a r)}], result)
24
+ end
25
+
26
+ test 'wrap' do
27
+ pipeline = each(%w(foo bar)) | wrap(each{ |x| x.chars}, :each)
28
+ expected = %w(r a b o o f)
29
+ while r = pipeline.run
30
+ assert_equal(expected.pop, r)
31
+ end
32
+ end
33
+
34
+ test 'unique and exhaust' do
35
+ pipeline = each([1, 3, 2, 3, 2, 1]) | unique | run_until_exhausted
36
+ assert_equal([1, 3, 2], pipeline.run)
37
+ end
38
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: stages
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.5
5
+ version: 0.2.6
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-18 00:00:00 Z
13
+ date: 2012-01-21 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -54,10 +54,12 @@ files:
54
54
  - lib/stages/select.rb
55
55
  - lib/stages/unique.rb
56
56
  - lib/stages/wrap.rb
57
+ - lib/sugar.rb
57
58
  - stages.gemspec
58
59
  - test/helper.rb
59
60
  - test/test_pipeline.rb
60
61
  - test/test_stages.rb
62
+ - test/test_syntax.rb
61
63
  homepage: https://github.com/iGoDigital-LLC/stages
62
64
  licenses: []
63
65
 
@@ -71,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
73
  requirements:
72
74
  - - ">="
73
75
  - !ruby/object:Gem::Version
74
- hash: 1313525796068718975
76
+ hash: 2434517779879848773
75
77
  segments:
76
78
  - 0
77
79
  version: "0"