summaryrefslogtreecommitdiff
path: root/lib/e2mmap.rb
blob: d10657bbad282f506fe48b15e18e995c69f4f347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
#   e2mmap.rb - for ruby 1.1
#   	$Release Version: 1.1$
#   	$Revision: 1.4 $
#   	$Date: 1997/08/18 07:12:12 $
#   	by Keiju ISHITSUKA
#
# --
#
#
if VERSION < "1.1"
  require "e2mmap1_0.rb"
else  
  
  module Exception2MessageMapper
    RCS_ID='-$Header: /home/keiju/var/src/var.lib/ruby/RCS/e2mmap.rb,v 1.4 1997/08/18 07:12:12 keiju Exp keiju $-'
    
    E2MM = Exception2MessageMapper
    
    def E2MM.extend_object(cl)
      super
      cl.bind(self)
    end
    
    # 以前との互換性のために残してある.
    def E2MM.extend_to(b)
      c = eval("self", b)
      c.extend(self)
    end
    
#    public :fail
    #    alias e2mm_fail fail

    def fail(err = nil, *rest)
      Exception2MessageMapper.fail Exception2MessageMapper::ErrNotRegisteredException, err.to_s
    end
    
    def bind(cl)
      self.module_eval %q^
	E2MM_ErrorMSG = {}
	# fail(err, *rest)
	#	err:	例外
	#	rest:	メッセージに渡すパラメータ
	#
	def self.fail(err = nil, *rest)
	  $@ = caller(0) if $@.nil?
	  $@.shift
	  if form = E2MM_ErrorMSG[err]
	    $! = err.new(sprintf(form, *rest))
	    # 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
	#	例外cのメッセージをmとする.
	#
	def self.def_e2message(c, m)
	  E2MM_ErrorMSG[c] = m
	end
	
	# def_exception(c, m)
	#	    n:  exception_name
	#	    m:  message_form
	#	    s:	例外スーパークラス(デフォルト: Exception)
	#	例外名``c''をもつ例外を定義し, そのメッセージをmとする.
	#
	#def def_exception(n, m)
	def self.def_exception(n, m, s = Exception)
	  n = n.id2name if n.kind_of?(Fixnum)
	  e = Class.new(s)
	  const_set(n, e)
	  E2MM_ErrorMSG[e] = m
	  #	const_get(:E2MM_ErrorMSG)[e] = m
	end
      ^
      end
      
      extend E2MM
      def_exception(:ErrNotClassOrModule, "Not Class or Module")
      def_exception(:ErrNotRegisteredException, "not registerd exception(%s)")
    end
end