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