Permalink
Browse files

initial import

  • Loading branch information...
0 parents commit c2593b9f02e547a74047897a943b684d779ed994 @archit committed Aug 20, 2008
Showing with 179 additions and 0 deletions.
  1. +5 −0 History.txt
  2. +6 −0 Manifest.txt
  3. +68 −0 README.txt
  4. +11 −0 Rakefile
  5. +28 −0 lib/restarts.rb
  6. +61 −0 test/test_restarts.rb
@@ -0,0 +1,5 @@
+=== 1.0.0 / 2008-08-21
+
+* 1 major enhancement
+ * Birthday!
+
@@ -0,0 +1,6 @@
+History.txt
+Manifest.txt
+README.txt
+Rakefile
+lib/restarts.rb
+test/test_restarts.rb
@@ -0,0 +1,68 @@
+= restarts
+
+http://github.com/archit/restarts/tree/master
+
+== 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.
+
+== FEATURES/PROBLEMS:
+
+* kicks ass
+
+== SYNOPSIS:
+
+def do_stuff()
+ error_occured = do_something()
+ if error_occured
+ raise_with_restarts(MyException.new(...)) { |restart_id|
+ case restart_id
+ when :ID1 then do_something_more()
+ when :ID2 then do_something_even_more()
+ end
+ }
+ end
+end
+
+begin
+ do_stuff()
+rescue MyException
+ $!.restart.call(:ID1) if (boolean expression inolving $! data)
+end
+
+== REQUIREMENTS:
+
+None whatsoever
+
+== INSTALL:
+
+sudo gem install restarts
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2008 Archit Baweja
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,11 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+require './lib/restarts.rb'
+
+Hoe.new('restarts', Restarts::VERSION) do |p|
+ p.developer('Archit Baweja', 'architbaweja@gmail.com')
+end
+
+# vim: syntax=Ruby
@@ -0,0 +1,28 @@
+# restarts.rb - Implements a simple condition-restarts exception system,
+# just like Common Lisp
+#
+# Refer to README.txt for more info
+
+module Restarts
+ VERSION = '1.0.0'
+end
+
+module Kernel
+ # FIXME: Find a way to have it part of Restarts module, but mixed-into Kernel
+ # Don't even know if thats a better way to do it.
+ def raise_with_restarts(condition)
+ restart = callcc { |cc|
+ # Have the continuation object accessible via the #restart method.
+ # FIXME: check for pre defined methods by the name "restart".
+ (class <<condition; self; end).class_eval { define_method(:restart) { return cc } }
+
+ raise condition
+ }
+
+ # This allows raise_condition to be used in the same way as regular raise
+ # Just don't give an exception.
+ #
+ # IDEA: maybe alias this to default Kernal#raise, would be nice.
+ yield restart if block_given?
+ end
+end
@@ -0,0 +1,61 @@
+# -*- ruby -*-
+require 'restarts'
+
+class RestartsTest < Test::Unit::TestCase
+
+ class ExampleError < RuntimeError
+ def initialize(a_message)
+ @message = a_message
+ end
+ end
+
+ class Example
+ attr_reader :restart1_used, :restart2_used
+
+ def initialize()
+ @restart1_used = false
+ @restart2_used = false
+ end
+
+ def i_will_raise_a_condition
+ raise_with_restarts(ExampleError.new("I'm really just a condition")) do |restart_at|
+ case restart_at
+ when :restart1 then @restart1_used = true
+ when :restart2 then @restart2_used = true
+ end
+ end
+ end
+ end
+
+ def setup
+ @ex = Example.new
+ end
+
+ def test_calls_raise
+ assert_raises(ExampleError) { @ex.i_will_raise_a_condition }
+ end
+
+ def test_restart1
+ begin
+ @ex.i_will_raise_a_condition
+ rescue ExampleError
+ $!.restart.call(:restart1)
+ end
+
+ assert @ex.restart1_used
+ assert !@ex.restart2_used
+ end
+
+ def test_restart2
+ begin
+ @ex.i_will_raise_a_condition
+ rescue ExampleError
+ $!.restart.call(:restart2)
+ end
+
+ assert !@ex.restart1_used
+ assert @ex.restart2_used
+ end
+end
+
+# vim: syntax=Ruby

0 comments on commit c2593b9

Please sign in to comment.