2 # e2mmap.rb - for ruby 1.1
3 # $Release Version: 2.0$
12 # extend Exception2MessageMapper
13 # def_e2message ExistingExceptionClass, "message..."
14 # def_exception :NewExceptionClass, "message..."[, superclass]
20 # extend Exception2MessageMapper
21 # def_e2meggage ExistingExceptionClass, "message..."
22 # def_exception :NewExceptionClass, "message..."[, superclass]
35 # extend Exception2MessageMapper
36 # def_e2message ExistingExceptionClass, "message..."
37 # def_exception :NewExceptionClass, "message..."[, superclass]
41 # extend Exception2MessageMapper
46 # Foo.Fail NewExceptionClass, arg...
47 # Foo.Fail ExistingExceptionClass, arg...
50 module Exception2MessageMapper
51 @RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
53 E2MM = Exception2MessageMapper
55 def E2MM.extend_object(cl)
57 cl.bind(self) unless cl < E2MM
62 def Raise(err = nil, *rest)
63 Exception2MessageMapper.Raise(self.class, err, *rest)
67 def self.included(mod)
68 mod.extend Exception2MessageMapper
75 # rest: message arguments
77 def Raise(err = nil, *rest)
78 E2MM.Raise(self, err, *rest)
86 # define exception c with message m.
88 def def_e2message(c, m)
89 E2MM.def_e2message(self, c, m)
92 # def_exception(n, m, s)
95 # s: superclass(default: StandardError)
96 # define exception named ``c'' with message m.
98 def def_exception(n, m, s = StandardError)
99 E2MM.def_exception(self, n, m, s)
103 # Private definitions.
105 # {[class, exp] => message, ...}
108 # E2MM.def_e2message(k, e, m)
109 # k: class to define exception under.
112 # define exception c with message m.
114 def E2MM.def_e2message(k, c, m)
115 E2MM.instance_eval{@MessageMap[[k, c]] = m}
119 # E2MM.def_exception(k, n, m, s)
120 # k: class to define exception under.
123 # s: superclass(default: StandardError)
124 # define exception named ``c'' with message m.
126 def E2MM.def_exception(k, n, m, s = StandardError)
127 n = n.id2name if n.kind_of?(Fixnum)
129 E2MM.instance_eval{@MessageMap[[k, e]] = m}
133 # Fail(klass, err, *rest)
134 # klass: class to define exception under.
136 # rest: message arguments
138 def E2MM.Raise(klass = E2MM, err = nil, *rest)
139 if form = e2mm_message(klass, err)
140 b = $@.nil? ? caller(1) : $@
143 b.shift if b[0] =~ /^#{Regexp.quote(__FILE__)}:/
144 raise err, sprintf(form, *rest), b
146 E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
153 def E2MM.e2mm_message(klass, exp)
154 for c in klass.ancestors
155 if mes = @MessageMap[[c,exp]]
157 m = klass.instance_eval('"' + mes + '"')
164 alias message e2mm_message
167 E2MM.def_exception(E2MM,
168 :ErrNotRegisteredException,
169 "not registerd exception(%s)")