sequencerrb 0.1.1 → 0.2.0
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/README.rdoc +8 -2
- data/VERSION +1 -1
- data/lib/collector.rb +29 -0
- data/lib/sequencer.rb +15 -2
- data/sequencerrb.gemspec +58 -0
- data/spec/collector_spec.rb +21 -0
- data/spec/sequencer_spec.rb +76 -10
- data/spec/spec_helper.rb +1 -0
- metadata +9 -5
data/README.rdoc
CHANGED
@@ -3,19 +3,25 @@
|
|
3
3
|
Simple library for sequencing procs/lambdas. Mainly useful for managing
|
4
4
|
sequential asynchronous operations.
|
5
5
|
|
6
|
+
== Install
|
7
|
+
|
8
|
+
~$ gem install sequencerrb
|
9
|
+
|
6
10
|
== Usage
|
7
11
|
|
8
12
|
acc = 0
|
9
13
|
|
10
|
-
f = proc { |
|
14
|
+
f = proc { |cb|
|
11
15
|
acc+=1
|
12
|
-
|
16
|
+
cb.call
|
13
17
|
}
|
14
18
|
|
15
19
|
Sequencer.new([f,f,f]).start
|
16
20
|
|
17
21
|
acc == 3 # true
|
18
22
|
|
23
|
+
Read the spec files for all the use cases.
|
24
|
+
|
19
25
|
== Note on Patches/Pull Requests
|
20
26
|
|
21
27
|
* Fork the project.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/collector.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
class Collector
|
2
|
+
|
3
|
+
attr_accessor :collect
|
4
|
+
|
5
|
+
def initialize(procs, fin)
|
6
|
+
@procs = procs
|
7
|
+
@finalize = fin
|
8
|
+
@counter = 0
|
9
|
+
@collect = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def add(f)
|
13
|
+
@procs.push(f)
|
14
|
+
end
|
15
|
+
|
16
|
+
def start
|
17
|
+
@procs.each do |f|
|
18
|
+
f.call(self)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def proc_finished
|
23
|
+
@counter += 1
|
24
|
+
if @procs.size == @counter
|
25
|
+
@finalize.call(self)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/sequencer.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# A class for running arbitrary actions in sequence.
|
2
2
|
class Sequencer
|
3
3
|
|
4
|
+
attr_reader :running
|
5
|
+
|
4
6
|
def initialize(procs=[])
|
5
|
-
@procs
|
7
|
+
@procs = procs
|
8
|
+
@running = false
|
6
9
|
end
|
7
10
|
|
8
11
|
# Add a function to the sequence
|
@@ -17,13 +20,23 @@ class Sequencer
|
|
17
20
|
|
18
21
|
# Start running
|
19
22
|
def start
|
23
|
+
@running = true
|
20
24
|
self.next
|
21
25
|
end
|
22
26
|
|
27
|
+
# Return a proc to trigger the start
|
28
|
+
def start_callback
|
29
|
+
proc {
|
30
|
+
self.start
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
23
34
|
# Run the next function (if any)
|
24
35
|
def next
|
25
36
|
if @procs.size > 0
|
26
|
-
@procs.shift.call(self)
|
37
|
+
@procs.shift.call(proc { self.next })
|
38
|
+
else
|
39
|
+
@running = false
|
27
40
|
end
|
28
41
|
end
|
29
42
|
|
data/sequencerrb.gemspec
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{sequencerrb}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Michiel Kalkman"]
|
12
|
+
s.date = %q{2010-08-06}
|
13
|
+
s.description = %q{Simple library for sequencing procs/lambdas. Mainly useful for managing sequential asynchronous operations.}
|
14
|
+
s.email = %q{michielkalkman@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/collector.rb",
|
27
|
+
"lib/sequencer.rb",
|
28
|
+
"sequencerrb.gemspec",
|
29
|
+
"spec/collector_spec.rb",
|
30
|
+
"spec/sequencer_spec.rb",
|
31
|
+
"spec/spec.opts",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/michiel/sequencer}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{Simple library for sequencing procs/lambdas.}
|
39
|
+
s.test_files = [
|
40
|
+
"spec/spec_helper.rb",
|
41
|
+
"spec/collector_spec.rb",
|
42
|
+
"spec/sequencer_spec.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Collector" do
|
4
|
+
|
5
|
+
it "should do something" do
|
6
|
+
f = proc {|st|
|
7
|
+
st.collect += 1
|
8
|
+
st.proc_finished
|
9
|
+
}
|
10
|
+
|
11
|
+
col = Collector.new(
|
12
|
+
[f,f,f],
|
13
|
+
proc {|st|
|
14
|
+
}
|
15
|
+
)
|
16
|
+
col.collect = 0
|
17
|
+
col.start
|
18
|
+
col.collect.should == 3
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/spec/sequencer_spec.rb
CHANGED
@@ -4,9 +4,9 @@ describe "Sequencer" do
|
|
4
4
|
it "should run three actions" do
|
5
5
|
|
6
6
|
acc = 0
|
7
|
-
f = proc { |
|
7
|
+
f = proc { |cb|
|
8
8
|
acc+=1
|
9
|
-
|
9
|
+
cb.call
|
10
10
|
}
|
11
11
|
Sequencer.new([f,f,f]).start
|
12
12
|
|
@@ -17,9 +17,9 @@ describe "Sequencer" do
|
|
17
17
|
it "should add two actions" do
|
18
18
|
|
19
19
|
acc = 0
|
20
|
-
f = proc { |
|
20
|
+
f = proc { |cb|
|
21
21
|
acc+=1
|
22
|
-
|
22
|
+
cb.call
|
23
23
|
}
|
24
24
|
|
25
25
|
seq = Sequencer.new
|
@@ -36,13 +36,13 @@ describe "Sequencer" do
|
|
36
36
|
acc = 0
|
37
37
|
|
38
38
|
seq = Sequencer.new([
|
39
|
-
proc { |
|
39
|
+
proc { |cb|
|
40
40
|
acc = 1
|
41
|
-
|
41
|
+
cb.call
|
42
42
|
},
|
43
|
-
proc { |
|
43
|
+
proc { |cb|
|
44
44
|
acc = 2
|
45
|
-
|
45
|
+
cb.call
|
46
46
|
}
|
47
47
|
])
|
48
48
|
seq.reverse!
|
@@ -55,10 +55,10 @@ describe "Sequencer" do
|
|
55
55
|
it "should work with nested lexical scope" do
|
56
56
|
|
57
57
|
acc = 0
|
58
|
-
f = proc { |
|
58
|
+
f = proc { |cb|
|
59
59
|
callback = proc {
|
60
60
|
acc+=1
|
61
|
-
|
61
|
+
cb.call
|
62
62
|
}
|
63
63
|
callback.call
|
64
64
|
}
|
@@ -69,4 +69,70 @@ describe "Sequencer" do
|
|
69
69
|
|
70
70
|
end
|
71
71
|
|
72
|
+
it "should work with threads" do
|
73
|
+
|
74
|
+
acc = 0
|
75
|
+
f = proc { |cb|
|
76
|
+
Thread.new do
|
77
|
+
acc+=1
|
78
|
+
cb.call
|
79
|
+
end
|
80
|
+
}
|
81
|
+
|
82
|
+
Sequencer.new([f,f,f]).start
|
83
|
+
|
84
|
+
acc.should == 3
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should fail threads with sleep because rspec doesn't do async" do
|
89
|
+
|
90
|
+
acc = 0
|
91
|
+
f = proc { |cb|
|
92
|
+
Thread.new do
|
93
|
+
sleep 5
|
94
|
+
acc+=1
|
95
|
+
cb.call
|
96
|
+
end
|
97
|
+
}
|
98
|
+
|
99
|
+
Sequencer.new([f,f,f]).start
|
100
|
+
|
101
|
+
acc.should == 0
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should allow nested usage" do
|
106
|
+
|
107
|
+
acc = 0
|
108
|
+
|
109
|
+
f = proc { |cb|
|
110
|
+
acc+=1
|
111
|
+
cb.call
|
112
|
+
}
|
113
|
+
|
114
|
+
Sequencer.new([ f, f, f,
|
115
|
+
Sequencer.new([f,f]).start_callback
|
116
|
+
]).start
|
117
|
+
|
118
|
+
acc.should == 5
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should have a working state accessor" do
|
123
|
+
|
124
|
+
acc = 0
|
125
|
+
|
126
|
+
f = proc { |cb|
|
127
|
+
acc+=1
|
128
|
+
cb.call
|
129
|
+
}
|
130
|
+
|
131
|
+
seq = Sequencer.new([ f, f, f ])
|
132
|
+
|
133
|
+
seq.start
|
134
|
+
seq.running.should == false
|
135
|
+
|
136
|
+
end
|
137
|
+
|
72
138
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequencerrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michiel Kalkman
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-06 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -50,7 +50,10 @@ files:
|
|
50
50
|
- README.rdoc
|
51
51
|
- Rakefile
|
52
52
|
- VERSION
|
53
|
+
- lib/collector.rb
|
53
54
|
- lib/sequencer.rb
|
55
|
+
- sequencerrb.gemspec
|
56
|
+
- spec/collector_spec.rb
|
54
57
|
- spec/sequencer_spec.rb
|
55
58
|
- spec/spec.opts
|
56
59
|
- spec/spec_helper.rb
|
@@ -90,4 +93,5 @@ specification_version: 3
|
|
90
93
|
summary: Simple library for sequencing procs/lambdas.
|
91
94
|
test_files:
|
92
95
|
- spec/spec_helper.rb
|
96
|
+
- spec/collector_spec.rb
|
93
97
|
- spec/sequencer_spec.rb
|