restarts 1.0.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/History.txt +15 -0
- data/Manifest.txt +6 -0
- data/README.txt +70 -0
- data/Rakefile +13 -0
- data/lib/restarts.rb +40 -0
- data/test/test_restarts.rb +71 -0
- metadata +71 -0
data/History.txt
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
=== 1.0.2 / 2008-08-21
|
2
|
+
|
3
|
+
* 1 minor enhancement
|
4
|
+
* Fixed up the ri/rdoc
|
5
|
+
|
6
|
+
=== 1.0.1 / 2008-08-21
|
7
|
+
|
8
|
+
* 1 minor enhancement
|
9
|
+
* Added syntax for directly calling the #restart method with a restart id
|
10
|
+
|
11
|
+
=== 1.0.0 / 2008-08-21
|
12
|
+
|
13
|
+
* 1 major enhancement
|
14
|
+
* Birthday!
|
15
|
+
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
= restarts
|
2
|
+
|
3
|
+
latest code is available at http://github.com/archit/restarts/tree/master
|
4
|
+
|
5
|
+
latest documentation is available at http://ore.rubyforge.org/restarts
|
6
|
+
|
7
|
+
== DESCRIPTION:
|
8
|
+
|
9
|
+
restarts.rb implements a new method Kernel#raise_condition which
|
10
|
+
is similar to Kernel#raise for throwing exceptions, with the added feature
|
11
|
+
of adding "restarts" next to the place where the exception is thrown so
|
12
|
+
that they can be invoked from the exception handler somewhere higher
|
13
|
+
up in the stack.
|
14
|
+
|
15
|
+
== FEATURES/PROBLEMS:
|
16
|
+
|
17
|
+
* kicks ass
|
18
|
+
|
19
|
+
== SYNOPSIS:
|
20
|
+
|
21
|
+
def do_stuff()
|
22
|
+
error_occured = do_something()
|
23
|
+
if error_occured
|
24
|
+
raise_with_restarts(MyException.new(...)) { |restart_id|
|
25
|
+
case restart_id
|
26
|
+
when :ID1 then do_something_more()
|
27
|
+
when :ID2 then do_something_even_more()
|
28
|
+
end
|
29
|
+
}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
begin
|
34
|
+
do_stuff()
|
35
|
+
rescue MyException
|
36
|
+
$!.restart(:ID1) if (boolean expression inolving $! data)
|
37
|
+
end
|
38
|
+
|
39
|
+
== REQUIREMENTS:
|
40
|
+
|
41
|
+
None whatsoever
|
42
|
+
|
43
|
+
== INSTALL:
|
44
|
+
|
45
|
+
gem install restarts
|
46
|
+
|
47
|
+
== LICENSE:
|
48
|
+
|
49
|
+
(The MIT License)
|
50
|
+
|
51
|
+
Copyright (c) 2008 Archit Baweja
|
52
|
+
|
53
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
54
|
+
a copy of this software and associated documentation files (the
|
55
|
+
'Software'), to deal in the Software without restriction, including
|
56
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
57
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
58
|
+
permit persons to whom the Software is furnished to do so, subject to
|
59
|
+
the following conditions:
|
60
|
+
|
61
|
+
The above copyright notice and this permission notice shall be
|
62
|
+
included in all copies or substantial portions of the Software.
|
63
|
+
|
64
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
65
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
66
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
67
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
68
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
69
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
70
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/restarts.rb'
|
6
|
+
|
7
|
+
Hoe.new('restarts', Restarts::VERSION) do |p|
|
8
|
+
p.rubyforge_name = 'ore'
|
9
|
+
p.developer('Archit Baweja', 'architbaweja@gmail.com')
|
10
|
+
p.remote_rdoc_dir = 'restarts'
|
11
|
+
end
|
12
|
+
|
13
|
+
# vim: syntax=Ruby
|
data/lib/restarts.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# restarts.rb - Implements a simple condition-restarts exception system,
|
2
|
+
# just like Common Lisp
|
3
|
+
#
|
4
|
+
# Refer to README.txt for more info
|
5
|
+
|
6
|
+
module Restarts
|
7
|
+
VERSION = '1.0.2'
|
8
|
+
end
|
9
|
+
|
10
|
+
module Kernel
|
11
|
+
# Similar to the standard Kernel#raise command. Allows you to throw exceptions
|
12
|
+
# The benefit is though that using raise_with_restarts adds the infrastructure
|
13
|
+
# so that the rescue code can tell the exception to restart and recover from
|
14
|
+
# a specific point.
|
15
|
+
#
|
16
|
+
# See README.txt for example
|
17
|
+
def raise_with_restarts(condition)
|
18
|
+
restart = callcc do |cc|
|
19
|
+
# Have the continuation object accessible via the #restart method.
|
20
|
+
# FIXME: check for pre defined methods by the name "restart".
|
21
|
+
(class <<condition; self; end).class_eval do
|
22
|
+
define_method(:restart) do |*id|
|
23
|
+
if id.empty?
|
24
|
+
return cc
|
25
|
+
else
|
26
|
+
return cc.call(id[0])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
raise condition
|
32
|
+
end
|
33
|
+
|
34
|
+
# This allows raise_condition to be used in the same way as regular raise
|
35
|
+
# Just don't give an exception.
|
36
|
+
#
|
37
|
+
# IDEA: maybe alias this to default Kernal#raise, would be nice.
|
38
|
+
yield restart if block_given?
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
require 'restarts'
|
3
|
+
|
4
|
+
class RestartsTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
class ExampleError < RuntimeError
|
7
|
+
def initialize(a_message)
|
8
|
+
@message = a_message
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Example
|
13
|
+
attr_reader :restart1_used, :restart2_used
|
14
|
+
|
15
|
+
def initialize()
|
16
|
+
@restart1_used = false
|
17
|
+
@restart2_used = false
|
18
|
+
end
|
19
|
+
|
20
|
+
def i_will_raise_a_condition
|
21
|
+
raise_with_restarts(ExampleError.new("I'm really just a condition")) do |restart_at|
|
22
|
+
case restart_at
|
23
|
+
when :restart1 then @restart1_used = true
|
24
|
+
when :restart2 then @restart2_used = true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def setup
|
31
|
+
@ex = Example.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_calls_raise
|
35
|
+
assert_raises(ExampleError) { @ex.i_will_raise_a_condition }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_restart1
|
39
|
+
begin
|
40
|
+
@ex.i_will_raise_a_condition
|
41
|
+
rescue ExampleError
|
42
|
+
$!.restart.call(:restart1)
|
43
|
+
end
|
44
|
+
|
45
|
+
assert @ex.restart1_used
|
46
|
+
assert !@ex.restart2_used
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_restart2
|
50
|
+
begin
|
51
|
+
@ex.i_will_raise_a_condition
|
52
|
+
rescue ExampleError
|
53
|
+
$!.restart.call(:restart2)
|
54
|
+
end
|
55
|
+
|
56
|
+
assert !@ex.restart1_used
|
57
|
+
assert @ex.restart2_used
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_alternative_syntax
|
61
|
+
begin
|
62
|
+
@ex.i_will_raise_a_condition
|
63
|
+
rescue ExampleError
|
64
|
+
$!.restart(:restart1)
|
65
|
+
end
|
66
|
+
|
67
|
+
assert @ex.restart1_used
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# vim: syntax=Ruby
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: restarts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Archit Baweja
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-22 00:00:00 +05:30
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.7.0
|
24
|
+
version:
|
25
|
+
description: restarts.rb implements a new method Kernel#raise_condition which is similar to Kernel#raise for throwing exceptions, with the added feature of adding "restarts" next to the place where the exception is thrown so that they can be invoked from the exception handler somewhere higher up in the stack.
|
26
|
+
email:
|
27
|
+
- architbaweja@gmail.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- Manifest.txt
|
39
|
+
- README.txt
|
40
|
+
- Rakefile
|
41
|
+
- lib/restarts.rb
|
42
|
+
- test/test_restarts.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: latest code is available at http://github.com/archit/restarts/tree/master
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --main
|
48
|
+
- README.txt
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: ore
|
66
|
+
rubygems_version: 1.2.0
|
67
|
+
signing_key:
|
68
|
+
specification_version: 2
|
69
|
+
summary: restarts.rb implements a new method Kernel#raise_condition which is similar to Kernel#raise for throwing exceptions, with the added feature of adding "restarts" next to the place where the exception is thrown so that they can be invoked from the exception handler somewhere higher up in the stack.
|
70
|
+
test_files:
|
71
|
+
- test/test_restarts.rb
|