genki-wormhole 0.1.1 → 0.1.2

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/ChangeLog CHANGED
@@ -1,3 +1,22 @@
1
+ == 0.1.2 / 2008-07-12
2
+
3
+ * Implemented the named wormhole. catch and throw methods now receive a name
4
+ parameter as the first param which is a symbol.
5
+ * Added an exception class to make a constraint for syntax
6
+
7
+ Now you must call return method after the catch block like this:
8
+
9
+ Wormhole.catch do
10
+ # something
11
+ end.return
12
+
13
+ This code raises Wormhole::LateReturnError
14
+
15
+ w = Wormhole.catch do
16
+ # something
17
+ end
18
+ w.return
19
+
1
20
  == 0.1.1 / 2008-07-04
2
21
 
3
22
  * changed API according to the suggestion of @yugui.
data/README CHANGED
@@ -19,6 +19,9 @@ depth of stack to outside.
19
19
 
20
20
  == Features/Problems
21
21
 
22
+ * Named wormhole
23
+ * Syntax constraint
24
+
22
25
  == Synopsis
23
26
 
24
27
  Here is an example of use.
@@ -30,10 +33,9 @@ Here is an example of use.
30
33
  Wormhole.throw
31
34
  end
32
35
 
33
- w = Wormhole.catch do
36
+ Wormhole.catch do
34
37
  some_method
35
- end
36
- w.return
38
+ end.return
37
39
 
38
40
  == Copyright
39
41
 
data/Rakefile CHANGED
@@ -9,6 +9,7 @@ require 'rake/contrib/rubyforgepublisher'
9
9
  require 'rake/contrib/sshpublisher'
10
10
  require 'rubyforge'
11
11
  require 'fileutils'
12
+ require 'lib/wormhole'
12
13
  include FileUtils
13
14
 
14
15
  NAME = "wormhole"
@@ -18,7 +19,7 @@ DESCRIPTION = "The utility library for making a wormhole on the stack fram
18
19
  RUBYFORGE_PROJECT = "cocktail-party"
19
20
  HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
20
21
  BIN_FILES = %w( )
21
- VERS = "0.1.1"
22
+ VERS = Wormhole::VERSION
22
23
 
23
24
  REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
24
25
  CLEAN.include ['**/.*.sw?', '*.gem', '.config']
@@ -59,7 +60,7 @@ spec = Gem::Specification.new do |s|
59
60
  s.autorequire = ""
60
61
  s.test_files = Dir["test/test_*.rb"]
61
62
 
62
- #s.add_dependency('activesupport', '>=1.3.1')
63
+ s.add_dependency('redgreen', '>=1.2.1')
63
64
  #s.required_ruby_version = '>= 1.8.2'
64
65
 
65
66
  s.files = %w(README ChangeLog Rakefile) +
@@ -135,3 +136,9 @@ desc 'Show information about the gem.'
135
136
  task :debug_gem do
136
137
  puts spec.to_ruby
137
138
  end
139
+
140
+
141
+ desc 'Update gem spec'
142
+ task :gemspec do
143
+ open("#{NAME}.gemspec", 'w').write spec.to_ruby
144
+ end
@@ -1,16 +1,30 @@
1
1
  class Wormhole
2
- def self.catch(&block)
2
+ VERSION = '0.1.2'
3
+ DEFAULT_NAME = :__wormhole__
4
+
5
+ class LateReturnError < SyntaxError
6
+ def initialize
7
+ super "You must call the return method at after the catch block."
8
+ end
9
+ end
10
+
11
+ def self.catch(name = DEFAULT_NAME, &block)
3
12
  result = nil
4
- wormhole = Kernel.catch(:__wormhole__) do
13
+ wormhole = Kernel.catch(name) do
5
14
  result = block.call if block
6
15
  nil
7
16
  end || new
8
17
  wormhole.instance_variable_set(:@result, result)
18
+ set_trace_func(proc do |event, file, line, id, binding, klass|
19
+ set_trace_func nil or raise LateReturnError if klass != Wormhole
20
+ set_trace_func nil if [event, id] == ['call', :return]
21
+ end)
9
22
  wormhole
10
23
  end
11
24
 
12
- def self.throw(data = {}, &block)
13
- new.send :connect, data, &block
25
+ def self.throw(*args, &block)
26
+ args.unshift DEFAULT_NAME unless args[0].is_a?(Symbol)
27
+ new.send :connect, *args, &block
14
28
  end
15
29
 
16
30
  # returns the result of the catch block.
@@ -22,14 +36,14 @@ class Wormhole
22
36
  end
23
37
 
24
38
  private
25
- def connect(data, &block)
39
+ def connect(name, data = {}, &block)
26
40
  @data = data
27
41
  callcc{|@cc|}
28
42
  if @opened
29
43
  @result = block.call @data if block
30
44
  @data
31
45
  else
32
- Kernel.throw :__wormhole__, self
46
+ Kernel.throw name, self
33
47
  end
34
48
  end
35
49
  end
@@ -1,7 +1,4 @@
1
1
  require 'test/unit'
2
2
  require 'rubygems'
3
- begin
4
- require 'redgreen'
5
- rescue Exception
6
- end
3
+ require 'redgreen'
7
4
  require File.dirname(__FILE__) + '/../lib/wormhole'
@@ -25,22 +25,47 @@ class WormholeTest < Test::Unit::TestCase
25
25
  end
26
26
 
27
27
  def test_wormhole_without_throw
28
- w = Wormhole.catch do
28
+ result = Wormhole.catch do
29
29
  "test"
30
+ end.return
31
+ assert_equal "test", result
32
+ end
33
+
34
+ def test_nested_wormhole
35
+ array = []
36
+ result = Wormhole.catch(:foo) do
37
+ Wormhole.catch(:bar) do
38
+ Wormhole.throw :foo, 1
39
+ Wormhole.throw :bar, 2
40
+ "test"
41
+ end.return do |value|
42
+ array << value
43
+ end
44
+ end.return do |value|
45
+ array << value
46
+ end
47
+ assert_equal [1, 2], array
48
+ assert_equal 'test', result
49
+ end
50
+
51
+ def test_wormhole_without_trailing_return
52
+ assert_raise Wormhole::LateReturnError do
53
+ w = Wormhole.catch do
54
+ "test"
55
+ end
56
+ w.return
30
57
  end
31
- assert_equal "test", w.return
32
58
  end
33
59
 
34
60
  def test_wormhole_without_symbol
35
- w = Wormhole.catch do
61
+ result = Wormhole.catch do
36
62
  @result << "foo"
37
63
  Wormhole.throw :bar => 'hello' do |data|
38
64
  @result << data[:bar]
39
65
  end
40
66
  @result << "bar"
41
67
  'result'
42
- end
43
- result = w.return do |data|
68
+ end.return do |data|
44
69
  @result << data[:bar]
45
70
  data[:bar] = 'world!'
46
71
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genki-wormhole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Takiuchi
@@ -9,10 +9,18 @@ autorequire: ""
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-02 00:00:00 -07:00
12
+ date: 2008-07-12 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: redgreen
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.1
23
+ version:
16
24
  description: The utility library for making a wormhole on the stack frame.
17
25
  email: genki@s21g.com
18
26
  executables: []