lib/sup/util.rb in sup-0.0.7 vs lib/sup/util.rb in sup-0.0.8

- old
+ new

@@ -6,14 +6,10 @@ def bool_accessor *args bool_reader(*args) bool_writer(*args) end - def attr_reader_cloned *args - args.each { |sym| class_eval %{ def #{sym}; @#{sym}.clone; end } } - end - def defer_all_other_method_calls_to obj class_eval %{ def method_missing meth, *a, &b; @#{obj}.send meth, *a, &b; end } end end @@ -265,15 +261,16 @@ ## simple singleton module. far less complete and insane than the ruby ## standard library one, but automatically forwards methods calls and ## allows for constructors that take arguments. ## ## You must have #initialize call "self.class.i_am_the_instance self" -## at some point or everything will fail horribly +## at some point or everything will fail horribly. module Singleton module ClassMethods def instance; @instance; end def instantiated?; defined?(@instance) && !@instance.nil?; end + def deinstantiate!; @instance = nil; end def method_missing meth, *a, &b raise "no instance defined!" unless defined? @instance @instance.send meth, *a, &b end def i_am_the_instance o @@ -282,7 +279,35 @@ end end def self.included klass klass.extend ClassMethods + end +end + +## wraps an object. if it throws an exception, keeps a copy, and +## rethrows it for any further method calls. +class Recoverable + def initialize o + @o = o + @e = nil + end + + def clear_error!; @e = nil; end + def has_errors?; !@e.nil?; end + def error; @e; end + + def method_missing m, *a, &b; __pass m, *a, &b; end + + def id; __pass :id; end + def to_s; __pass :to_s; end + def to_yaml x; __pass :to_yaml, x; end + + def __pass m, *a, &b + begin + @o.send(m, *a, &b) + rescue Exception => e + @e = e + raise e + end end end