wormhole 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Rakefile +1 -1
  2. data/lib/wormhole.rb +28 -9
  3. data/test/wormhole_test.rb +35 -13
  4. 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.3"
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 < Exception
2
- def initialize(data = {})
3
- @data = data
4
- callcc{|@cc|}
5
- raise self unless @return
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
- def close
9
- @return = true
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
- def [](key) @data[key] end
14
- def []=(key, value) @data[key] = value end
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
@@ -8,23 +8,45 @@ class WormholeTest < Test::Unit::TestCase
8
8
 
9
9
  def foo
10
10
  @result << "foo"
11
- w = Wormhole.new :foo => 'hello'
12
- @result << w[:foo]
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
- foo
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
- 'foo',
21
- 'hello',
22
- 'world!',
23
- 'bar',
24
- ], @result
25
- rescue Wormhole => w
26
- @result << w[:foo]
27
- w[:foo] = 'world!'
28
- w.close
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wormhole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Takiuchi