wormhole 0.0.3 → 0.1.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/Rakefile +1 -1
- data/lib/wormhole.rb +28 -9
- data/test/wormhole_test.rb +35 -13
- metadata +1 -1
data/Rakefile
CHANGED
@@ -18,7 +18,7 @@ DESCRIPTION = "The utility library for making a wormhole on the stack fram
|
|
18
18
|
RUBYFORGE_PROJECT = "cocktail-party"
|
19
19
|
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
20
20
|
BIN_FILES = %w( )
|
21
|
-
VERS = "0.0
|
21
|
+
VERS = "0.1.0"
|
22
22
|
|
23
23
|
REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
|
24
24
|
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
|
data/lib/wormhole.rb
CHANGED
@@ -1,15 +1,34 @@
|
|
1
|
-
class Wormhole
|
2
|
-
def
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
class Wormhole
|
2
|
+
def self.catch(&block)
|
3
|
+
result = nil
|
4
|
+
wormhole = Kernel.catch(:__wormhole__) do
|
5
|
+
result = block.call if block
|
6
|
+
nil
|
7
|
+
end || new
|
8
|
+
wormhole.instance_variable_set(:@result, result)
|
9
|
+
wormhole
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.throw(data = {}, &block)
|
13
|
+
new.send :connect, data, &block
|
6
14
|
end
|
7
15
|
|
8
|
-
|
9
|
-
|
16
|
+
# returns the result of the catch block.
|
17
|
+
def return(&block)
|
18
|
+
return @result if @cc.nil?
|
19
|
+
block.call @data if block
|
20
|
+
@opened = true
|
10
21
|
@cc.call
|
11
22
|
end
|
12
23
|
|
13
|
-
|
14
|
-
def
|
24
|
+
private
|
25
|
+
def connect(data, &block)
|
26
|
+
@data = data
|
27
|
+
callcc{|@cc|}
|
28
|
+
if @opened
|
29
|
+
@result = block.call @data if block
|
30
|
+
else
|
31
|
+
Kernel.throw :__wormhole__, self
|
32
|
+
end
|
33
|
+
end
|
15
34
|
end
|
data/test/wormhole_test.rb
CHANGED
@@ -8,23 +8,45 @@ class WormholeTest < Test::Unit::TestCase
|
|
8
8
|
|
9
9
|
def foo
|
10
10
|
@result << "foo"
|
11
|
-
|
12
|
-
|
11
|
+
Wormhole.throw :bar => 'hello' do |data|
|
12
|
+
@result << data[:bar]
|
13
|
+
end
|
13
14
|
@result << "bar"
|
14
15
|
end
|
15
16
|
|
16
17
|
def test_wormhole
|
17
|
-
|
18
|
+
Wormhole.catch do
|
19
|
+
foo
|
20
|
+
end.return do |data|
|
21
|
+
@result << data[:bar]
|
22
|
+
data[:bar] = 'world!'
|
23
|
+
end
|
18
24
|
assert !@result.empty?
|
19
|
-
assert_equal [
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
assert_equal ['foo', 'hello', 'world!', 'bar'], @result
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_wormhole_without_throw
|
29
|
+
w = Wormhole.catch do
|
30
|
+
"test"
|
31
|
+
end
|
32
|
+
assert_equal "test", w.return
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_wormhole_without_symbol
|
36
|
+
w = Wormhole.catch do
|
37
|
+
@result << "foo"
|
38
|
+
Wormhole.throw :bar => 'hello' do |data|
|
39
|
+
@result << data[:bar]
|
40
|
+
end
|
41
|
+
@result << "bar"
|
42
|
+
'result'
|
43
|
+
end
|
44
|
+
result = w.return do |data|
|
45
|
+
@result << data[:bar]
|
46
|
+
data[:bar] = 'world!'
|
47
|
+
end
|
48
|
+
assert_equal 'result', result
|
49
|
+
assert !@result.empty?
|
50
|
+
assert_equal ['foo', 'hello', 'world!', 'bar'], @result
|
29
51
|
end
|
30
52
|
end
|