datapipes 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/CHANGELOG.md +4 -0
- data/README.md +1 -1
- data/lib/datapipes/basics/print.rb +1 -1
- data/lib/datapipes/basics/triple.rb +5 -1
- data/lib/datapipes/composable.rb +5 -2
- data/lib/datapipes/sink.rb +2 -5
- data/lib/datapipes/tube.rb +7 -12
- data/lib/datapipes/version.rb +1 -1
- data/lib/datapipes.rb +2 -1
- data/spec/composable_spec.rb +0 -65
- data/spec/sink_spec.rb +71 -0
- data/spec/tube_spec.rb +44 -0
- metadata +7 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36fcf4de0a57bf1712ee266c6d99272ae5b823bf
|
4
|
+
data.tar.gz: 3c88fe0667cf777f8241cbf57c4f729046645092
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d79a8457b86cbc3f9e12128e7e08ef840d7e90fd9f230cc57c48e41b7997570dfec5865c60ee0256ccfc511cc2e6a985dda7038821379d59fd61863e17fb1c6f
|
7
|
+
data.tar.gz: 0a5a5a692c50f1814affee9caaf132e0c142951733147d7683c02ba83daa3db5c4ec0e4d13d2c74449408438293b5d8b410628856eac37aec4afd5cd83c2f0f4
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-c
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
data/lib/datapipes/composable.rb
CHANGED
@@ -2,9 +2,12 @@ class Datapipes
|
|
2
2
|
module Composable
|
3
3
|
attr_accessor :accumulated
|
4
4
|
|
5
|
-
def +(
|
5
|
+
def +(op2)
|
6
|
+
op1 = self
|
7
|
+
op1_acc = (op1.accumulated || [op1])
|
8
|
+
op2_acc = (op2.accumulated || [op2])
|
6
9
|
self.class.new.tap do |o|
|
7
|
-
o.accumulated =
|
10
|
+
o.accumulated = op1_acc + op2_acc
|
8
11
|
end
|
9
12
|
end
|
10
13
|
end
|
data/lib/datapipes/sink.rb
CHANGED
@@ -3,13 +3,10 @@ class Datapipes
|
|
3
3
|
class Sink
|
4
4
|
include Composable
|
5
5
|
|
6
|
+
# TODO: parallel
|
6
7
|
def run_all(data)
|
7
8
|
@accumulated ||= [self]
|
8
|
-
@accumulated.each {|sink| sink.run(data)
|
9
|
-
end
|
10
|
-
|
11
|
-
def accept?(data)
|
12
|
-
true
|
9
|
+
@accumulated.each {|sink| sink.run(data) }
|
13
10
|
end
|
14
11
|
end
|
15
12
|
end
|
data/lib/datapipes/tube.rb
CHANGED
@@ -3,22 +3,17 @@ class Datapipes
|
|
3
3
|
#
|
4
4
|
# Build your own tube logic in `run` method.
|
5
5
|
class Tube
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
@accumulated.reduce(data) do |d, tube|
|
12
|
-
if tube.accept? d
|
13
|
-
tube.run(d)
|
14
|
-
else
|
15
|
-
d
|
6
|
+
def >>(op2)
|
7
|
+
op1 = self
|
8
|
+
Tube.new.tap do |o|
|
9
|
+
o.define_singleton_method(:run) do |data|
|
10
|
+
op2.run(op1.run(data))
|
16
11
|
end
|
17
12
|
end
|
18
13
|
end
|
19
14
|
|
20
|
-
def
|
21
|
-
|
15
|
+
def run(data)
|
16
|
+
data
|
22
17
|
end
|
23
18
|
end
|
24
19
|
end
|
data/lib/datapipes/version.rb
CHANGED
data/lib/datapipes.rb
CHANGED
@@ -6,6 +6,7 @@ require 'datapipes/pipe'
|
|
6
6
|
require 'datapipes/version'
|
7
7
|
|
8
8
|
class Datapipes
|
9
|
+
# TODO: optional tube and pipe
|
9
10
|
def initialize(source, tube, sink, pipe)
|
10
11
|
@source = source
|
11
12
|
@tube = tube
|
@@ -34,7 +35,7 @@ class Datapipes
|
|
34
35
|
loop do
|
35
36
|
break if resource_ended? && @pipe.empty?
|
36
37
|
|
37
|
-
data = @tube.
|
38
|
+
data = @tube.run(@pipe.pull)
|
38
39
|
@sink.run_all(data)
|
39
40
|
end
|
40
41
|
Thread.current.kill
|
data/spec/composable_spec.rb
CHANGED
@@ -1,69 +1,4 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Datapipes::Composable do
|
4
|
-
context 'with valid object' do
|
5
|
-
let(:class_a) do
|
6
|
-
Class.new do
|
7
|
-
include Datapipes::Composable
|
8
|
-
|
9
|
-
def call
|
10
|
-
one
|
11
|
-
end
|
12
|
-
|
13
|
-
def one
|
14
|
-
1
|
15
|
-
end
|
16
|
-
|
17
|
-
def exec
|
18
|
-
accumulated.map(&:call)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
let(:class_b) do
|
24
|
-
Class.new do
|
25
|
-
include Datapipes::Composable
|
26
|
-
|
27
|
-
def call
|
28
|
-
five
|
29
|
-
end
|
30
|
-
|
31
|
-
def five
|
32
|
-
5
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
let(:a) { class_a.new }
|
38
|
-
let(:b) { class_b.new }
|
39
|
-
subject { a + b }
|
40
|
-
|
41
|
-
it 'remember defined body' do
|
42
|
-
expect(subject.exec).to eq [1, 5]
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context 'with tube' do
|
47
|
-
let(:tube_a) do
|
48
|
-
Class.new(Datapipes::Tube) do
|
49
|
-
def run(data)
|
50
|
-
data + 2
|
51
|
-
end
|
52
|
-
end.new
|
53
|
-
end
|
54
|
-
|
55
|
-
let(:tube_b) do
|
56
|
-
Class.new(Datapipes::Tube) do
|
57
|
-
def run(data)
|
58
|
-
data * 3
|
59
|
-
end
|
60
|
-
end.new
|
61
|
-
end
|
62
|
-
|
63
|
-
subject { tube_a + tube_b }
|
64
|
-
|
65
|
-
it 'generates new tube' do
|
66
|
-
expect(subject.run_all(4)).to eq 18
|
67
|
-
end
|
68
|
-
end
|
69
4
|
end
|
data/spec/sink_spec.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Datapipes::Sink do
|
4
|
+
describe 'composable' do
|
5
|
+
context 'with 3 sinks' do
|
6
|
+
subject { sink_a + sink_b + sink_c }
|
7
|
+
|
8
|
+
it 'composes' do
|
9
|
+
subject.run_all(5)
|
10
|
+
expect(list).to eq [8, 20, 6]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'with 4 sinks' do
|
15
|
+
subject { sink_a + sink_b + sink_c + sink_d }
|
16
|
+
|
17
|
+
it 'composes' do
|
18
|
+
subject.run_all(5)
|
19
|
+
expect(list).to eq [8, 20, 6, 50]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'associative law' do
|
24
|
+
let(:a) { (sink_a + sink_b) + sink_c }
|
25
|
+
let(:b) { sink_a + (sink_b + sink_c) }
|
26
|
+
|
27
|
+
it 'keeps' do
|
28
|
+
expect(a.run_all(3)).to eq b.run_all(3)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
let(:list) { [] }
|
33
|
+
|
34
|
+
let(:sink_a) do
|
35
|
+
list_ = list
|
36
|
+
Class.new(Datapipes::Sink) do
|
37
|
+
define_method(:run) do |data|
|
38
|
+
list_ << (data + 3)
|
39
|
+
end
|
40
|
+
end.new
|
41
|
+
end
|
42
|
+
|
43
|
+
let(:sink_b) do
|
44
|
+
list_ = list
|
45
|
+
Class.new(Datapipes::Sink) do
|
46
|
+
define_method(:run) do |data|
|
47
|
+
list_ << (data * 4)
|
48
|
+
end
|
49
|
+
end.new
|
50
|
+
end
|
51
|
+
|
52
|
+
let(:sink_c) do
|
53
|
+
list_ = list
|
54
|
+
Class.new(Datapipes::Sink) do
|
55
|
+
define_method(:run) do |data|
|
56
|
+
list_ << (data + 1)
|
57
|
+
end
|
58
|
+
end.new
|
59
|
+
end
|
60
|
+
|
61
|
+
let(:sink_d) do
|
62
|
+
list_ = list
|
63
|
+
Class.new(Datapipes::Sink) do
|
64
|
+
define_method(:run) do |data|
|
65
|
+
list_ << (data * 10)
|
66
|
+
end
|
67
|
+
end.new
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
data/spec/tube_spec.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Datapipes::Tube do
|
4
|
+
describe 'composable' do
|
5
|
+
let(:tube_a) do
|
6
|
+
Class.new(Datapipes::Tube) do
|
7
|
+
def run(data)
|
8
|
+
data + 2
|
9
|
+
end
|
10
|
+
end.new
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:tube_b) do
|
14
|
+
Class.new(Datapipes::Tube) do
|
15
|
+
def run(data)
|
16
|
+
data * 3
|
17
|
+
end
|
18
|
+
end.new
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:tube_c) do
|
22
|
+
Class.new(Datapipes::Tube) do
|
23
|
+
def run(data)
|
24
|
+
data + 4
|
25
|
+
end
|
26
|
+
end.new
|
27
|
+
end
|
28
|
+
|
29
|
+
subject { tube_a >> tube_b >> tube_c }
|
30
|
+
|
31
|
+
it 'composes' do
|
32
|
+
expect(subject.run(4)).to eq 22
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'associative law' do
|
36
|
+
let(:a) { (tube_a >> tube_b) >> tube_c }
|
37
|
+
let(:b) { tube_a >> (tube_b >> tube_c) }
|
38
|
+
|
39
|
+
it 'keeps' do
|
40
|
+
expect(a.run(5)).to eq b.run(5)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: datapipes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Taiki ONO
|
@@ -74,7 +74,9 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
|
+
- ".rspec"
|
77
78
|
- ".travis.yml"
|
79
|
+
- CHANGELOG.md
|
78
80
|
- Gemfile
|
79
81
|
- LICENSE
|
80
82
|
- README.md
|
@@ -94,7 +96,9 @@ files:
|
|
94
96
|
- lib/datapipes/version.rb
|
95
97
|
- spec/basics_spec.rb
|
96
98
|
- spec/composable_spec.rb
|
99
|
+
- spec/sink_spec.rb
|
97
100
|
- spec/spec_helper.rb
|
101
|
+
- spec/tube_spec.rb
|
98
102
|
homepage: https://github.com/taiki45/datapipes
|
99
103
|
licenses:
|
100
104
|
- MIT
|
@@ -122,5 +126,7 @@ summary: An asynchronous multi steamings library.
|
|
122
126
|
test_files:
|
123
127
|
- spec/basics_spec.rb
|
124
128
|
- spec/composable_spec.rb
|
129
|
+
- spec/sink_spec.rb
|
125
130
|
- spec/spec_helper.rb
|
131
|
+
- spec/tube_spec.rb
|
126
132
|
has_rdoc:
|