lib/e2mmap.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #   e2mmap.rb - for ruby 1.1
   3  #       $Release Version: 2.0$
   4  #       $Revision: 1.10 $
   5  #       $Date: 1999/02/17 12:33:17 $
   6  #       by Keiju ISHITSUKA
   7  #
   8  # --
   9  #   Usage:
  10  #
  11  # U1)
  12  #   class Foo
  13  #     extend Exception2MessageMapper
  14  #     def_e2message ExistingExceptionClass, "message..."
  15  #     def_exception :NewExceptionClass, "message..."[, superclass]
  16  #     ...
  17  #   end
  18  #
  19  # U2)
  20  #   module Error
  21  #     extend Exception2MessageMapper
  22  #     def_e2meggage ExistingExceptionClass, "message..."
  23  #     def_exception :NewExceptionClass, "message..."[, superclass]
  24  #     ...
  25  #   end
  26  #   class Foo
  27  #     include Error
  28  #     ...
  29  #   end
  30  #
  31  #   foo = Foo.new
  32  #   foo.Fail ....
  33  #
  34  # U3)
  35  #   module Error
  36  #     extend Exception2MessageMapper
  37  #     def_e2message ExistingExceptionClass, "message..."
  38  #     def_exception :NewExceptionClass, "message..."[, superclass]
  39  #     ...
  40  #   end
  41  #   class Foo
  42  #     extend Exception2MessageMapper
  43  #     include Error
  44  #     ...
  45  #   end
  46  #
  47  #   Foo.Fail NewExceptionClass, arg...
  48  #   Foo.Fail ExistingExceptionClass, arg...
  49  #
  50  #
  51  fail "Use Ruby 1.1" if VERSION < "1.1"
  52  
  53  module Exception2MessageMapper
  54    @RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
  55  
  56    E2MM = Exception2MessageMapper
  57  
  58    def E2MM.extend_object(cl)
  59      super
  60      cl.bind(self) unless cl == E2MM
  61    end
  62    
  63    # backward compatibility
  64    def E2MM.extend_to(b)
  65      c = eval("self", b)
  66      c.extend(self)
  67    end
  68  
  69    def bind(cl)
  70      self.module_eval %[
  71        def Raise(err = nil, *rest)
  72          Exception2MessageMapper.Raise(self.type, err, *rest)
  73        end
  74        alias Fail Raise
  75  
  76        def self.included(mod)
  77          mod.extend Exception2MessageMapper
  78        end
  79      ]
  80    end
  81  
  82    # Fail(err, *rest)
  83    #     err:    exception
  84    #     rest:   message arguments
  85    #
  86    def Raise(err = nil, *rest)
  87      E2MM.Raise(self, err, *rest)
  88    end
  89    alias Fail Raise
  90  
  91    # backward compatibility
  92    alias fail! fail
  93    def fail(err = nil, *rest)
  94      begin 
  95        E2MM.Fail(self, err, *rest)
  96      rescue E2MM::ErrNotRegisteredException
  97        super
  98      end
  99    end
 100    class << self
 101      public :fail
 102    end
 103  
 104    
 105    # def_e2message(c, m)
 106    #         c:  exception
 107    #         m:  message_form
 108    #     define exception c with message m.
 109    #
 110    def def_e2message(c, m)
 111      E2MM.def_e2message(self, c, m)
 112    end
 113    
 114    # def_exception(n, m, s)
 115    #         n:  exception_name
 116    #         m:  message_form
 117    #         s:  superclass(default: StandardError)
 118    #     define exception named ``c'' with message m.
 119    #
 120    def def_exception(n, m, s = StandardError)
 121      E2MM.def_exception(self, n, m, s)
 122    end
 123  
 124    #
 125    # Private definitions.
 126    #
 127    # {[class, exp] => message, ...}
 128    @MessageMap = {}
 129  
 130    # E2MM.def_exception(k, e, m)
 131    #         k:  class to define exception under.
 132    #         e:  exception
 133    #         m:  message_form
 134    #     define exception c with message m.
 135    #
 136    def E2MM.def_e2message(k, c, m)
 137      E2MM.instance_eval{@MessageMap[[k, c]] = m}
 138      c
 139    end
 140    
 141    # E2MM.def_exception(k, n, m, s)
 142    #         k:  class to define exception under.
 143    #         n:  exception_name
 144    #         m:  message_form
 145    #         s:  superclass(default: StandardError)
 146    #     define exception named ``c'' with message m.
 147    #
 148    def E2MM.def_exception(k, n, m, s = StandardError)
 149      n = n.id2name if n.kind_of?(Fixnum)
 150      e = Class.new(s)
 151      E2MM.instance_eval{@MessageMap[[k, e]] = m}
 152      k.const_set(n, e)
 153    end
 154  
 155    # Fail(klass, err, *rest)
 156    #     klass:  class to define exception under.
 157    #     err:    exception
 158    #     rest:   message arguments
 159    #
 160    def E2MM.Raise(klass = E2MM, err = nil, *rest)
 161      if form = e2mm_message(klass, err)
 162        $! = err.new(sprintf(form, *rest))
 163        $@ = caller(1) if $@.nil?
 164        #p $@
 165        #p __FILE__
 166        $@.shift if $@[0] =~ /^#{Regexp.quote(__FILE__)}:/
 167        raise
 168      else
 169        E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
 170      end
 171    end
 172    class <<E2MM
 173      alias Fail Raise
 174    end
 175  
 176    def E2MM.e2mm_message(klass, exp)
 177      for c in klass.ancestors
 178        if mes = @MessageMap[[c,exp]]
 179          #p mes
 180          m = klass.instance_eval('"' + mes + '"')
 181          return m
 182        end
 183      end
 184      nil
 185    end
 186    class <<self
 187      alias message e2mm_message
 188    end
 189  
 190    E2MM.def_exception(E2MM, 
 191                       :ErrNotRegisteredException, 
 192                       "not registerd exception(%s)")
 193  end
 194  
 195