# e2mmap.rb - for Ruby 1.1
# Helper module for easily defining exceptions with predefined messages.
# extend Exception2MessageMapper
# def_e2message ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
# extend Exception2MessageMapper
# def_e2message ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
# extend Exception2MessageMapper
# def_e2message ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
# extend Exception2MessageMapper
# Foo.Fail NewExceptionClass, arg...
# Foo.Fail ExistingExceptionClass, arg...
module Exception2MessageMapper
E2MM = Exception2MessageMapper # :nodoc:
def E2MM.extend_object(cl)
cl.bind(self) unless cl < E2MM
def Raise(err = nil, *rest)
Exception2MessageMapper.Raise(self.class, err, *rest)
mod.extend Exception2MessageMapper
# rest: message arguments
def Raise(err = nil, *rest)
E2MM.Raise(self, err, *rest)
# define exception c with message m.
E2MM.def_e2message(self, c, m)
# s: superclass(default: StandardError)
# define exception named ``c'' with message m.
def def_exception(n, m, s = StandardError)
E2MM.def_exception(self, n, m, s)
# {[class, exp] => message, ...}
# E2MM.def_e2message(k, e, m)
# k: class to define exception under.
# define exception c with message m.
def E2MM.def_e2message(k, c, m)
E2MM.instance_eval{@MessageMap[[k, c]] = m}
# E2MM.def_exception(k, n, m, s)
# k: class to define exception under.
# s: superclass(default: StandardError)
# define exception named ``c'' with message m.
def E2MM.def_exception(k, n, m, s = StandardError)
n = n.id2name if n.kind_of?(Fixnum)
E2MM.instance_eval{@MessageMap[[k, e]] = m}
# Fail(klass, err, *rest)
# klass: class to define exception under.
# rest: message arguments
def E2MM.Raise(klass = E2MM, err = nil, *rest)
if form = e2mm_message(klass, err)
b = $@.nil? ? caller(1) : $@
b.shift if b[0] =~ /^#{Regexp.quote(__FILE__)}:/
raise err, sprintf(form, *rest), b
E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
def E2MM.e2mm_message(klass, exp)
if mes = @MessageMap[[c,exp]]
m = klass.instance_eval('"' + mes + '"')
alias message e2mm_message
:ErrNotRegisteredException,
"not registered exception(%s)")