Module: Exception2MessageMapper

Included in:
ExceptionForMatrix
Defined in:
opal/stdlib/e2mmap.rb

Overview

-- e2mmap.rb - for Ruby 1.1 $Release Version: 2.0$ $Revision: 1.10 $ by Keiju ISHITSUKA

++

Helper module for easily defining exceptions with predefined messages.

== Usage

1. class Foo extend Exception2MessageMapper def_e2message ExistingExceptionClass, "message..." def_exception :NewExceptionClass, "message..."[, superclass] ... end

2. module Error extend Exception2MessageMapper def_e2message ExistingExceptionClass, "message..." def_exception :NewExceptionClass, "message..."[, superclass] ... end class Foo include Error ... end

foo = Foo.new foo.Fail ....

3. module Error extend Exception2MessageMapper def_e2message ExistingExceptionClass, "message..." def_exception :NewExceptionClass, "message..."[, superclass] ... end class Foo extend Exception2MessageMapper include Error ... end

Foo.Fail NewExceptionClass, arg... Foo.Fail ExistingExceptionClass, arg...

Constant Summary

E2MM =

:nodoc:

Exception2MessageMapper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.def_e2message(k, c, m) ⇒ Object

E2MM.def_e2message(k, e, m) k: class to define exception under. e: exception m: message_form define exception c with message m.



121
122
123
124
# File 'opal/stdlib/e2mmap.rb', line 121

def E2MM.def_e2message(k, c, m)
  E2MM.instance_eval{@MessageMap[[k, c]] = m}
  c
end

.def_exception(k, n, m, s = StandardError) ⇒ Object

E2MM.def_exception(k, n, m, s) k: class to define exception under. n: exception_name m: message_form s: superclass(default: StandardError) define exception named ``c'' with message m.



133
134
135
136
137
138
# File 'opal/stdlib/e2mmap.rb', line 133

def E2MM.def_exception(k, n, m, s = StandardError)
  e = Class.new(s)
  E2MM.instance_eval{@MessageMap[[k, e]] = m}
  k.module_eval {remove_const(n)} if k.const_defined?(n, false)
  k.const_set(n, e)
end

.e2mm_message(klass, exp) ⇒ Object Also known as: message



158
159
160
161
162
163
164
165
166
167
168
# File 'opal/stdlib/e2mmap.rb', line 158

def E2MM.e2mm_message(klass, exp)
  klass.ancestors.each do |c|
    if mes = @MessageMap[[c,exp]]
      # PATCH: who needs this hack?
      # m = klass.instance_eval('"' + mes + '"')
      # return m
      return mes
    end
  end
  nil
end

.extend_object(cl) ⇒ Object



59
60
61
62
# File 'opal/stdlib/e2mmap.rb', line 59

def E2MM.extend_object(cl)
  super
  cl.bind(self) unless cl < E2MM
end

.Raise(klass = E2MM, err = nil, *rest) ⇒ Object Also known as: Fail

Fail(klass, err, *rest) klass: class to define exception under. err: exception rest: message arguments



145
146
147
148
149
150
151
152
153
# File 'opal/stdlib/e2mmap.rb', line 145

def E2MM.Raise(klass = E2MM, err = nil, *rest)
  if form = e2mm_message(klass, err)
    b = $@.nil? ? caller(1) : $@
    b.shift if b[0] =~ /^#{Regexp.quote(__FILE__)}:/
    raise err, sprintf(form, *rest), b
  else
    E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
  end
end

Instance Method Details

#bind(cl) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'opal/stdlib/e2mmap.rb', line 64

def bind(cl)
  self.module_eval do
    def Raise(err = nil, *rest)
      Exception2MessageMapper.Raise(self.class, err, *rest)
    end
    alias Fail Raise

    class << self
      undef included
    end
    def self.included(mod)
      mod.extend Exception2MessageMapper
    end
  end
end

#def_e2message(c, m) ⇒ Object

def_e2message(c, m) c: exception m: message_form define exception c with message m.



95
96
97
# File 'opal/stdlib/e2mmap.rb', line 95

def def_e2message(c, m)
  E2MM.def_e2message(self, c, m)
end

#def_exception(n, m, s = StandardError) ⇒ Object

def_exception(n, m, s) n: exception_name m: message_form s: superclass(default: StandardError) define exception named ``c'' with message m.



105
106
107
# File 'opal/stdlib/e2mmap.rb', line 105

def def_exception(n, m, s = StandardError)
  E2MM.def_exception(self, n, m, s)
end

#failObject

Fail(err, *rest) err: exception rest: message arguments



88
89
90
# File 'opal/stdlib/e2mmap.rb', line 88

def Raise(err = nil, *rest)
  E2MM.Raise(self, err, *rest)
end

#FailObject

Fail(err, *rest) err: exception rest: message arguments



87
88
89
# File 'opal/stdlib/e2mmap.rb', line 87

def Raise(err = nil, *rest)
  E2MM.Raise(self, err, *rest)
end

#Raise(err = nil, *rest) ⇒ Object

Fail(err, *rest) err: exception rest: message arguments



84
85
86
# File 'opal/stdlib/e2mmap.rb', line 84

def Raise(err = nil, *rest)
  E2MM.Raise(self, err, *rest)
end