summaryrefslogtreecommitdiff
path: root/lib/e2mmap.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/e2mmap.rb')
-rw-r--r--lib/e2mmap.rb269
1 files changed, 167 insertions, 102 deletions
diff --git a/lib/e2mmap.rb b/lib/e2mmap.rb
index bf860dc5c1..e04ed4a5b4 100644
--- a/lib/e2mmap.rb
+++ b/lib/e2mmap.rb
@@ -1,17 +1,46 @@
#
# e2mmap.rb - for ruby 1.1
-# $Release Version: 1.2$
-# $Revision: 1.8 $
-# $Date: 1998/08/19 15:22:22 $
+# $Release Version: 2.0$
+# $Revision: 1.10 $
+# $Date: 1999/02/17 12:33:17 $
# by Keiju ISHITSUKA
#
# --
# Usage:
#
+# U1)
# class Foo
# extend Exception2MassageMapper
+# def_e2message ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
+# ...
+# end
+#
+# U2)
+# module Error
+# extend Exception2MassageMapper
# def_e2meggage ExistingExceptionClass, "message..."
+# def_exception :NewExceptionClass, "message..."[, superclass]
+# ...
+# end
+# class Foo
+# include Exp
+# ...
+# end
+#
+# foo = Foo.new
+# foo.Fail ....
+#
+# U3)
+# module Error
+# extend Exception2MassageMapper
+# def_e2message ExistingExceptionClass, "message..."
+# def_exception :NewExceptionClass, "message..."[, superclass]
+# ...
+# end
+# class Foo
+# extend Exception2MessageMapper
+# include Error
# ...
# end
#
@@ -19,113 +48,149 @@
# Foo.Fail ExistingExceptionClass, arg...
#
#
-if VERSION < "1.1"
- require "e2mmap1_0.rb"
-else
+fail "Use Ruby 1.1" if VERSION < "1.1"
+
+module Exception2MessageMapper
+ @RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
+
+ E2MM = Exception2MessageMapper
+
+ def E2MM.extend_object(cl)
+ super
+ cl.bind(self) unless cl == E2MM
+ end
- module Exception2MessageMapper
- @RCS_ID='-$Id: e2mmap.rb,v 1.8 1998/08/19 15:22:22 keiju Exp keiju $-'
-
- E2MM = Exception2MessageMapper
+ # 以前との互換性のために残してある.
+ def E2MM.extend_to(b)
+ c = eval("self", b)
+ c.extend(self)
+ end
- def E2MM.extend_object(cl)
+ def bind(cl)
+ self.module_eval %[
+ def Raise(err = nil, *rest)
+ Exception2MessageMapper.Raise(self.type, err, *rest)
+ end
+ alias Fail Raise
+
+ def self.append_features(mod)
+ super
+ mod.extend Exception2MessageMapper
+ end
+ ]
+ end
+
+ # Fail(err, *rest)
+ # err: 例外
+ # rest: メッセージに渡すパラメータ
+ #
+ def Raise(err = nil, *rest)
+ E2MM.Raise(self, err, *rest)
+ end
+ alias Fail Raise
+
+ # 過去の互換性のため
+ alias fail! fail
+ def fail(err = nil, *rest)
+ begin
+ E2MM.Fail(self, err, *rest)
+ rescue E2MM::ErrNotRegisteredException
super
- cl.bind(self)
end
-
- # backward compatibility
- def E2MM.extend_to(b)
- c = eval("self", b)
- c.extend(self)
- end
-
- # public :fail
- alias fail! fail
+ end
+ class << self
+ public :fail
+ end
- #def fail(err = nil, *rest)
- # super
- #end
+
+ # def_e2message(c, m)
+ # c: exception
+ # m: message_form
+ # 例外cのメッセージをmとする.
+ #
+ def def_e2message(c, m)
+ E2MM.def_e2message(self, c, m)
+ end
+
+ # def_exception(c, m)
+ # n: exception_name
+ # m: message_form
+ # s: 例外スーパークラス(デフォルト: StandardError)
+ # 例外名``c''をもつ例外を定義し, そのメッセージをmとする.
+ #
+ def def_exception(n, m, s = StandardError)
+ E2MM.def_exception(self, n, m, s)
+ end
- def Fail(err = nil, *rest)
- Exception2MessageMapper.Fail Exception2MessageMapper::ErrNotRegisteredException, err.inspect
- end
-
- def bind(cl)
- self.module_eval %q^
- E2MM_ErrorMSG = {} unless self.const_defined?(:E2MM_ErrorMSG)
- # fail(err, *rest)
- # err: Exception
- # rest: Parameter accompanied with the exception
- #
- def self.Fail(err = nil, *rest)
- if form = E2MM_ErrorMSG[err]
- $! = err.new(sprintf(form, *rest))
- $@ = caller(0) if $@.nil?
- $@.shift
- # e2mm_fail()
- raise()
-# elsif self == Exception2MessageMapper
-# fail Exception2MessageMapper::ErrNotRegisteredException, err.to_s
- else
-# print "super\n"
- super
- end
- end
+ #
+ # Private definitions.
+ #
+ # {[class, exp] => message, ...}
+ @MessageMap = {}
+
+ # E2MM.def_exception(k, e, m)
+ # k: 例外を定義するクラス
+ # e: exception
+ # m: message_form
+ # 例外cのメッセージをmとする.
+ #
+ def E2MM.def_e2message(k, c, m)
+ E2MM.instance_eval{@MessageMap[[k, c]] = m}
+ c
+ end
+
+ # E2MM.def_exception(k, c, m)
+ # k: 例外を定義するクラス
+ # n: exception_name
+ # m: message_form
+ # s: 例外スーパークラス(デフォルト: StandardError)
+ # 例外名``c''をもつ例外を定義し, そのメッセージをmとする.
+ #
+ 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
- # 過去の互換性のため
- def self.fail(err = nil, *rest)
- if form = E2MM_ErrorMSG[err]
- $! = err.new(sprintf(form, *rest))
- $@ = caller(0) if $@.nil?
- $@.shift
- # e2mm_fail()
- raise()
-# elsif self == Exception2MessageMapper
-# fail Exception2MessageMapper::ErrNotRegisteredException, err.to_s
- else
-# print "super\n"
- super
- end
- end
- class << self
- public :fail
- end
-
- # def_exception(c, m)
- # c: exception
- # m: message_form
- #
- def self.def_e2message(c, m)
- E2MM_ErrorMSG[c] = m
- end
-
- # def_exception(c, m)
- # n: exception_name
- # m: message_form
- # s: superclass_of_exception (default: Exception)
- # defines excaption named ``c'', whose message is ``m''.
- #
- #def def_exception(n, m)
- def self.def_exception(n, m, s = nil)
- n = n.id2name if n.kind_of?(Fixnum)
- unless s
- if defined?(StandardError)
- s = StandardError
- else
- s = Exception
- end
- end
- e = Class.new(s)
+ # Fail(klass, err, *rest)
+ # klass: 例外の定義されているクラス
+ # err: 例外
+ # rest: メッセージに渡すパラメータ
+ #
+ def E2MM.Raise(klass = E2MM, err = nil, *rest)
+ if form = e2mm_message(klass, err)
+ $! = err.new(sprintf(form, *rest))
+ $@ = caller(1) if $@.nil?
+ #p $@
+ #p __FILE__
+ $@.shift if $@[0] =~ /^#{Regexp.quote(__FILE__)}:/
+ raise
+ else
+ E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
+ end
+ end
+ class <<E2MM
+ alias Fail Raise
+ end
- const_set(n, e)
- E2MM_ErrorMSG[e] = m
- # const_get(:E2MM_ErrorMSG)[e] = m
- end
- ^
+ def E2MM.e2mm_message(klass, exp)
+ for c in klass.ancestors
+ if mes = @MessageMap[[c,exp]]
+ p mes
+ m = klass.instance_eval('"' + mes + '"')
+ return m
end
-
- extend E2MM
- def_exception(:ErrNotClassOrModule, "Not Class or Module")
- def_exception(:ErrNotRegisteredException, "not registerd exception(%s)")
end
+ nil
+ end
+ class <<self
+ alias message e2mm_message
+ end
+
+ E2MM.def_exception(E2MM,
+ :ErrNotRegisteredException,
+ "not registerd exception(%s)")
end
+
+