em_alldone 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +4 -0
- data/README.rdoc +18 -6
- data/lib/em_alldone.rb +41 -1
- data/spec/em_alldone_spec.rb +37 -0
- metadata +3 -3
data/CHANGELOG.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -12,15 +12,28 @@ success or failure. Doesn't support errbacks.
|
|
12
12
|
|
13
13
|
A quick example:
|
14
14
|
|
15
|
+
require 'eventmachine'
|
15
16
|
require 'em_alldone'
|
16
17
|
|
17
18
|
d1 = EM::DefaultDeferrable.new
|
18
19
|
d2 = EM::DefaultDeferrable.new
|
19
|
-
EmAlldone.new(d1, d2) do
|
20
|
-
puts "All done"
|
20
|
+
EmAlldone.new(d1, d2) do |arg|
|
21
|
+
puts "All done #{arg}"
|
21
22
|
end
|
22
|
-
|
23
|
-
|
23
|
+
d2.succeed 2 # Nothing happens
|
24
|
+
d1.succeed 1 # Prints out "All done 2"
|
25
|
+
|
26
|
+
d1 = EM::DefaultDeferrable.new
|
27
|
+
d2 = EM::DefaultDeferrable.new
|
28
|
+
str = "Hello"
|
29
|
+
d1.callback do
|
30
|
+
str.upcase!
|
31
|
+
end
|
32
|
+
EmAlldone.with([d1, d2], str) do |arg|
|
33
|
+
puts "All done #{arg}"
|
34
|
+
end
|
35
|
+
d2.succeed 2 # Nothing happens
|
36
|
+
d1.succeed 1 # Prints out "All done HELLO"
|
24
37
|
|
25
38
|
|
26
39
|
== REQUIREMENTS:
|
@@ -32,8 +45,7 @@ gem installed and required.
|
|
32
45
|
|
33
46
|
== INSTALL:
|
34
47
|
|
35
|
-
|
36
|
-
|
48
|
+
gem install em_alldone
|
37
49
|
|
38
50
|
== RUNNING THE SPECS:
|
39
51
|
|
data/lib/em_alldone.rb
CHANGED
@@ -3,7 +3,47 @@
|
|
3
3
|
# success or failure. Doesn't support errbacks.
|
4
4
|
class EmAlldone
|
5
5
|
module Version # :nodoc:
|
6
|
-
STRING = '0.0.
|
6
|
+
STRING = '0.0.2'
|
7
|
+
end
|
8
|
+
|
9
|
+
# This is a super simple implementation of EM::DefaultDeferrable.
|
10
|
+
# This is used so we don't need to explicitly depend upon the EventMachine gem.
|
11
|
+
# This is not meant for external use.
|
12
|
+
class BasicDeferrable # :nodoc:
|
13
|
+
# Does nothing
|
14
|
+
def errback; end
|
15
|
+
def fail(*args); end
|
16
|
+
|
17
|
+
# If the callback arguments are set (i.e. `succeed` has been called),
|
18
|
+
# then the given callback will be run immediately.
|
19
|
+
def callback &block
|
20
|
+
@callback = block
|
21
|
+
if @callback_args
|
22
|
+
@callback.call *@callback_args
|
23
|
+
end
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
# If the callback is set, then it will be run.
|
28
|
+
# Otherwise we save the arguments for later.
|
29
|
+
def succeed(*args)
|
30
|
+
if @callback
|
31
|
+
@callback.call(*args)
|
32
|
+
else
|
33
|
+
@callback_args = args
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
# Returns an instance. However, it ensures that when the callback is called
|
41
|
+
# it is called with the specified arguments.
|
42
|
+
def self.with(deferrables, *args, &block)
|
43
|
+
deferrables = deferrables.flatten
|
44
|
+
deferrables << BasicDeferrable.new
|
45
|
+
deferrables[-1].succeed *args
|
46
|
+
new(deferrables, &block)
|
7
47
|
end
|
8
48
|
|
9
49
|
def initialize(*deferrables, &block)
|
data/spec/em_alldone_spec.rb
CHANGED
@@ -52,4 +52,41 @@ describe EmAlldone do
|
|
52
52
|
@it_ran.should be_true
|
53
53
|
end
|
54
54
|
end
|
55
|
+
|
56
|
+
context "the arguments to the callback" do
|
57
|
+
let(:d1) { EM::DefaultDeferrable.new }
|
58
|
+
let(:d2) { EM::DefaultDeferrable.new }
|
59
|
+
before do
|
60
|
+
EmAlldone.new(d1, d2) do |*args|
|
61
|
+
@it_ran = args
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "implicitly set" do
|
66
|
+
it "is the arguments of the last deferrable's succeed" do
|
67
|
+
d2.succeed 2, "two"
|
68
|
+
d1.succeed 1
|
69
|
+
@it_ran.should == [2, "two"]
|
70
|
+
end
|
71
|
+
|
72
|
+
it "is the arguments of the last deferrable's fail" do
|
73
|
+
d1.fail 1, "one"
|
74
|
+
d2.fail 2
|
75
|
+
@it_ran.should == [2]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "explicitly set" do
|
80
|
+
it "is the explicitly set argements" do
|
81
|
+
EmAlldone.with([d1, d2], "explicitly", :set) do |*args|
|
82
|
+
@it_ran = args
|
83
|
+
end
|
84
|
+
|
85
|
+
d1.succeed 1
|
86
|
+
d2.fail 2, "two"
|
87
|
+
|
88
|
+
@it_ran.should == ["explicitly", :set]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
55
92
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em_alldone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
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: 2014-
|
12
|
+
date: 2014-08-01 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Provides a way to execute a callback when all of a set of deferrables
|
15
15
|
complete.
|
@@ -54,7 +54,7 @@ rubyforge_project:
|
|
54
54
|
rubygems_version: 1.8.28
|
55
55
|
signing_key:
|
56
56
|
specification_version: 3
|
57
|
-
summary: em_alldone0.0.
|
57
|
+
summary: em_alldone0.0.2
|
58
58
|
test_files:
|
59
59
|
- spec/em_alldone_spec.rb
|
60
60
|
- spec/spec_helper.rb
|