1998-01-16 07:13:05 -05:00
|
|
|
#
|
2013-05-20 09:01:24 -04:00
|
|
|
#--
|
2013-07-18 09:50:32 -04:00
|
|
|
# e2mmap.rb - for Ruby 1.1
|
2011-05-18 20:07:25 -04:00
|
|
|
# $Release Version: 2.0$
|
|
|
|
# $Revision: 1.10 $
|
|
|
|
# by Keiju ISHITSUKA
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2013-05-20 09:01:24 -04:00
|
|
|
#++
|
1999-01-19 23:59:39 -05:00
|
|
|
#
|
2013-05-20 09:01:24 -04:00
|
|
|
# Helper module for easily defining exceptions with predefined messages.
|
|
|
|
#
|
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# 1.
|
1999-01-19 23:59:39 -05:00
|
|
|
# class Foo
|
2000-09-21 22:37:13 -04:00
|
|
|
# extend Exception2MessageMapper
|
1999-08-13 01:45:20 -04:00
|
|
|
# def_e2message ExistingExceptionClass, "message..."
|
1999-01-19 23:59:39 -05:00
|
|
|
# def_exception :NewExceptionClass, "message..."[, superclass]
|
1999-08-13 01:45:20 -04:00
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
#
|
2013-05-20 09:01:24 -04:00
|
|
|
# 2.
|
1999-08-13 01:45:20 -04:00
|
|
|
# module Error
|
2000-09-21 22:37:13 -04:00
|
|
|
# extend Exception2MessageMapper
|
1999-01-19 23:59:39 -05:00
|
|
|
# def_e2meggage ExistingExceptionClass, "message..."
|
1999-08-13 01:45:20 -04:00
|
|
|
# def_exception :NewExceptionClass, "message..."[, superclass]
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
# class Foo
|
2000-09-21 22:37:13 -04:00
|
|
|
# include Error
|
1999-08-13 01:45:20 -04:00
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# foo = Foo.new
|
|
|
|
# foo.Fail ....
|
|
|
|
#
|
2013-05-20 09:01:24 -04:00
|
|
|
# 3.
|
1999-08-13 01:45:20 -04:00
|
|
|
# module Error
|
2000-09-21 22:37:13 -04:00
|
|
|
# extend Exception2MessageMapper
|
1999-08-13 01:45:20 -04:00
|
|
|
# def_e2message ExistingExceptionClass, "message..."
|
|
|
|
# def_exception :NewExceptionClass, "message..."[, superclass]
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
# class Foo
|
|
|
|
# extend Exception2MessageMapper
|
|
|
|
# include Error
|
1999-01-19 23:59:39 -05:00
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Foo.Fail NewExceptionClass, arg...
|
|
|
|
# Foo.Fail ExistingExceptionClass, arg...
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
#
|
1999-08-13 01:45:20 -04:00
|
|
|
module Exception2MessageMapper
|
|
|
|
@RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
|
|
|
|
|
2013-05-20 09:01:24 -04:00
|
|
|
E2MM = Exception2MessageMapper # :nodoc:
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
def E2MM.extend_object(cl)
|
|
|
|
super
|
2007-12-15 01:03:22 -05:00
|
|
|
cl.bind(self) unless cl < E2MM
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
def bind(cl)
|
|
|
|
self.module_eval %[
|
|
|
|
def Raise(err = nil, *rest)
|
2011-05-18 20:07:25 -04:00
|
|
|
Exception2MessageMapper.Raise(self.class, err, *rest)
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
|
|
|
alias Fail Raise
|
|
|
|
|
2001-05-06 11:06:00 -04:00
|
|
|
def self.included(mod)
|
2011-05-18 20:07:25 -04:00
|
|
|
mod.extend Exception2MessageMapper
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Fail(err, *rest)
|
2011-05-18 20:07:25 -04:00
|
|
|
# err: exception
|
|
|
|
# rest: message arguments
|
1999-08-13 01:45:20 -04:00
|
|
|
#
|
|
|
|
def Raise(err = nil, *rest)
|
|
|
|
E2MM.Raise(self, err, *rest)
|
|
|
|
end
|
|
|
|
alias Fail Raise
|
2008-01-09 09:23:34 -05:00
|
|
|
alias fail Raise
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
# def_e2message(c, m)
|
2011-05-18 20:07:25 -04:00
|
|
|
# c: exception
|
|
|
|
# m: message_form
|
|
|
|
# define exception c with message m.
|
1999-08-13 01:45:20 -04:00
|
|
|
#
|
|
|
|
def def_e2message(c, m)
|
|
|
|
E2MM.def_e2message(self, c, m)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-09-21 22:37:13 -04:00
|
|
|
# def_exception(n, m, s)
|
2011-05-18 20:07:25 -04:00
|
|
|
# n: exception_name
|
|
|
|
# m: message_form
|
|
|
|
# s: superclass(default: StandardError)
|
|
|
|
# define exception named ``c'' with message m.
|
1999-08-13 01:45:20 -04:00
|
|
|
#
|
|
|
|
def def_exception(n, m, s = StandardError)
|
|
|
|
E2MM.def_exception(self, n, m, s)
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
#
|
|
|
|
# Private definitions.
|
|
|
|
#
|
|
|
|
# {[class, exp] => message, ...}
|
|
|
|
@MessageMap = {}
|
|
|
|
|
2008-06-16 01:46:28 -04:00
|
|
|
# E2MM.def_e2message(k, e, m)
|
2011-05-18 20:07:25 -04:00
|
|
|
# k: class to define exception under.
|
|
|
|
# e: exception
|
|
|
|
# m: message_form
|
|
|
|
# define exception c with message m.
|
1999-08-13 01:45:20 -04:00
|
|
|
#
|
|
|
|
def E2MM.def_e2message(k, c, m)
|
|
|
|
E2MM.instance_eval{@MessageMap[[k, c]] = m}
|
|
|
|
c
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-09-21 22:37:13 -04:00
|
|
|
# E2MM.def_exception(k, n, m, s)
|
2011-05-18 20:07:25 -04:00
|
|
|
# k: class to define exception under.
|
|
|
|
# n: exception_name
|
|
|
|
# m: message_form
|
|
|
|
# s: superclass(default: StandardError)
|
|
|
|
# define exception named ``c'' with message m.
|
1999-08-13 01:45:20 -04:00
|
|
|
#
|
|
|
|
def E2MM.def_exception(k, n, m, s = StandardError)
|
|
|
|
n = n.id2name if n.kind_of?(Fixnum)
|
|
|
|
e = Class.new(s)
|
|
|
|
E2MM.instance_eval{@MessageMap[[k, e]] = m}
|
|
|
|
k.const_set(n, e)
|
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
# Fail(klass, err, *rest)
|
2011-05-18 20:07:25 -04:00
|
|
|
# klass: class to define exception under.
|
|
|
|
# err: exception
|
|
|
|
# rest: message arguments
|
1999-08-13 01:45:20 -04:00
|
|
|
#
|
|
|
|
def E2MM.Raise(klass = E2MM, err = nil, *rest)
|
|
|
|
if form = e2mm_message(klass, err)
|
2007-12-15 01:03:22 -05:00
|
|
|
b = $@.nil? ? caller(1) : $@
|
1999-08-13 01:45:20 -04:00
|
|
|
#p $@
|
|
|
|
#p __FILE__
|
2007-12-15 01:03:22 -05:00
|
|
|
b.shift if b[0] =~ /^#{Regexp.quote(__FILE__)}:/
|
2011-05-18 20:07:25 -04:00
|
|
|
raise err, sprintf(form, *rest), b
|
1999-08-13 01:45:20 -04:00
|
|
|
else
|
|
|
|
E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
|
|
|
|
end
|
|
|
|
end
|
2010-04-27 17:09:46 -04:00
|
|
|
class << E2MM
|
1999-08-13 01:45:20 -04:00
|
|
|
alias Fail Raise
|
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
def E2MM.e2mm_message(klass, exp)
|
|
|
|
for c in klass.ancestors
|
|
|
|
if mes = @MessageMap[[c,exp]]
|
2011-05-18 17:19:18 -04:00
|
|
|
#p mes
|
|
|
|
m = klass.instance_eval('"' + mes + '"')
|
|
|
|
return m
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
1999-08-13 01:45:20 -04:00
|
|
|
nil
|
|
|
|
end
|
2010-04-27 17:09:46 -04:00
|
|
|
class << self
|
1999-08-13 01:45:20 -04:00
|
|
|
alias message e2mm_message
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
E2MM.def_exception(E2MM,
|
2011-05-18 17:19:18 -04:00
|
|
|
:ErrNotRegisteredException,
|
|
|
|
"not registerd exception(%s)")
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
|