pvc 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +9 -0
- data/lib/pvc/lines_piece.rb +51 -0
- data/lib/pvc/pipeline.rb +11 -0
- data/lib/pvc/version.rb +1 -1
- data/spec/pvc/integration_spec.rb +10 -0
- metadata +3 -2
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -45,6 +45,15 @@ This code is packaged as a Gem. If you like, you can build and install it by run
|
|
45
45
|
upcase_unique_pipeline = PVC.new("tr", "a-z", "A-Z").to("uniq")
|
46
46
|
PVC.new.input("hello\nHeLlO\nworld\nWoRlD\n").to(upcase_unique_pipeline).to("sort", "-r").run.stdout # => "WORLD\nHELLO"
|
47
47
|
|
48
|
+
# Shorthand for linewise rewriting
|
49
|
+
PVC.new.input("hello\nworld").lines_map { |l| l.upcase }.run.stdout "HELLO\nWORLD"
|
50
|
+
|
51
|
+
# Shorthand for linewise processing (without modifying the stream)
|
52
|
+
count = 0
|
53
|
+
result = PVC.new.input("hello\nworld").lines_tap { |l| count += 1 }.run
|
54
|
+
count # => 2
|
55
|
+
result.stdout # => "hello\nworld"
|
56
|
+
|
48
57
|
## Synopsis - unimplemented
|
49
58
|
|
50
59
|
# Kill run if it does not finish in time (miliseconds)
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module PVC
|
2
|
+
class LinesPiece
|
3
|
+
|
4
|
+
class Runner
|
5
|
+
|
6
|
+
def initialize(block, opts)
|
7
|
+
@block = block
|
8
|
+
@mode = opts[:mode]
|
9
|
+
@read, @write = IO.pipe
|
10
|
+
@read.close_on_exec = true
|
11
|
+
@write.close_on_exec = true
|
12
|
+
end
|
13
|
+
|
14
|
+
def stdin
|
15
|
+
@write
|
16
|
+
end
|
17
|
+
|
18
|
+
def start(following_piece)
|
19
|
+
@return = nil
|
20
|
+
@thread = Thread.new do
|
21
|
+
@read.each_line do |line|
|
22
|
+
new_line = @block.call(line)
|
23
|
+
following_piece.stdin.write case @mode
|
24
|
+
when :map then new_line
|
25
|
+
when :tap then line
|
26
|
+
else raise "wrong mode"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def finish
|
33
|
+
@write.close
|
34
|
+
@thread.join
|
35
|
+
@read.close
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize(block, opts)
|
41
|
+
@block = block
|
42
|
+
@mode = opts[:mode]
|
43
|
+
end
|
44
|
+
|
45
|
+
def runner
|
46
|
+
Runner.new(@block, :mode => @mode)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/lib/pvc/pipeline.rb
CHANGED
@@ -7,6 +7,7 @@ require "pvc/with_err_piece"
|
|
7
7
|
require "pvc/only_err_piece"
|
8
8
|
require "pvc/result_piece"
|
9
9
|
require "pvc/input_piece"
|
10
|
+
require "pvc/lines_piece"
|
10
11
|
require "pvc/result"
|
11
12
|
|
12
13
|
module PVC
|
@@ -45,6 +46,16 @@ module PVC
|
|
45
46
|
self
|
46
47
|
end
|
47
48
|
|
49
|
+
def lines_map(&block)
|
50
|
+
@pieces << LinesPiece.new(block, :mode => :map)
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def lines_tap(&block)
|
55
|
+
@pieces << LinesPiece.new(block, :mode => :tap)
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
48
59
|
def run
|
49
60
|
runners = ([NullPiece.new] + @pieces + [ResultPiece.new]).map(&:runner)
|
50
61
|
|
data/lib/pvc/version.rb
CHANGED
@@ -57,6 +57,16 @@ describe "pvc" do
|
|
57
57
|
PVC.new.input(string).to(upcase_unique_pipeline).to("sort", "-r").run.stdout.should == "WORLD\nHELLO\n"
|
58
58
|
end
|
59
59
|
|
60
|
+
it "should have a shorthand method for processing each line with a block" do
|
61
|
+
PVC.new.input("hello\nworld").lines_map { |l| l.upcase }.run.stdout.should == "HELLO\nWORLD"
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should have a shorthand for linewise processing (without modifying the stream)" do
|
65
|
+
count = 0
|
66
|
+
PVC.new.input("hello\nworld").lines_tap { |l| count += 1 }.run.stdout.should == "hello\nworld"
|
67
|
+
count.should == 2
|
68
|
+
end
|
69
|
+
|
60
70
|
end
|
61
71
|
|
62
72
|
describe "(original manual tests)" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pvc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: childprocess
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- lib/pvc.rb
|
60
60
|
- lib/pvc/block_piece.rb
|
61
61
|
- lib/pvc/input_piece.rb
|
62
|
+
- lib/pvc/lines_piece.rb
|
62
63
|
- lib/pvc/null_piece.rb
|
63
64
|
- lib/pvc/only_err_piece.rb
|
64
65
|
- lib/pvc/pipeline.rb
|