123456789_123456789_123456789_123456789_123456789_

Module: Exception2MessageMapper

Relationships & Source Files
Defined in: lib/e2mmap.rb

Overview

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...

Class Method Summary

Instance Method Summary

Class Method Details

.def_e2message(k, c, m)

E2MM.def_e2message(k, e, m)

k:  class to define exception under.
    e:  exception
    m:  message_form
define exception c with message m.
[ GitHub ]

  
# File 'lib/e2mmap.rb', line 117

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

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

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.
[ GitHub ]

  
# File 'lib/e2mmap.rb', line 129

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

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

[ GitHub ]

  
# File 'lib/e2mmap.rb', line 154

def E2MM.e2mm_message(klass, exp)
  for c in klass.ancestors
    if mes = @MessageMap[[c,exp]]
      m = klass.instance_eval('"' + mes + '"')
      return m
    end
  end
  nil
end

.extend_object(cl)

[ GitHub ]

  
# File 'lib/e2mmap.rb', line 58

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

Fail(klass = E2MM, err = nil, *rest)

Alias for .Raise.

[ GitHub ]

  
# File 'lib/e2mmap.rb', line 151

alias Fail Raise

.message(klass, exp)

Alias for .e2mm_message.

[ GitHub ]

  
# File 'lib/e2mmap.rb', line 164

alias message e2mm_message

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

Fail(klass, err, *rest)

klass:  class to define exception under.
err:    exception
rest:   message arguments
[ GitHub ]

  
# File 'lib/e2mmap.rb', line 141

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)

[ GitHub ]

  
# File 'lib/e2mmap.rb', line 63

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

    def self.included(mod)
      mod.extend Exception2MessageMapper
    end
  ]
end

#def_e2message(c, m)

def_e2message(c, m)

c:  exception
    m:  message_form
define exception c with message m.
[ GitHub ]

  
# File 'lib/e2mmap.rb', line 91

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

#def_exception(n, m, s = StandardError)

def_exception(n, m, s)

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

  
# File 'lib/e2mmap.rb', line 101

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

Fail(err = nil, *rest)

Alias for #Raise.

[ GitHub ]

  
# File 'lib/e2mmap.rb', line 83

alias Fail Raise

#fail(err = nil, *rest)

Alias for #Raise.

[ GitHub ]

  
# File 'lib/e2mmap.rb', line 84

alias fail Raise

Raise(err = nil, *rest) Also known as: #Fail, #fail

Fail(err, *rest)

err:    exception
rest:   message arguments
[ GitHub ]

  
# File 'lib/e2mmap.rb', line 80

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