Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/define_exception.rb

Overview

Simple mixin that provides a way of defining custom exception classes with default messages that dries up your code.

Class Methods

define_exception( ExceptionName, ‘Default Error Message’ )

The exception name can either be a string or a symbol. If it is a string then the name will be as provided. In the symbol case if the symbol is mixed case the result exception name will be as provided. You also have the option of using underscores and having the method automatically generate the mixed case exception name:

define_exception 'MyError', 'The Default Message'
define_exception :AnotherError, 'Another Default Message'
define_exception :worst_error, 'Worst Default Message'

Any of the above methods will work.

Usage

The standard ruby practice of requiring the gem and then use include DefineException to make the class method available.

Example

require 'rubygems'
require 'define_exception'

include DefineException

class Test
  define_exception :test_me, 'this is the default message'

  def test
    raise TestMe
  end

  def test2
    raise( TestMe, 'You shall not do that again' )
  end
end

t = Test.new

begin
  t.test
rescue Test::TestMe => e
  puts e.message
end

t.test2

running the above example would correctly produce

wes:~/define_exception/examples> ruby sample.rb
this is the default message
test.rb:14:in `test2': You shall not do that again (Test::TestMe)
    from test.rb:26

Information

Author

Wes Bailey, [email protected]

License

Ruby License

Instance Method Summary collapse

Instance Method Details

#lame_camel_caseObject



66
67
68
# File 'lib/define_exception.rb', line 66

def lame_camel_case
  /_/.match( self ) ? self.to_s.split( '_' ).inject( '' ) { |s,v| s << v.capitalize } : self
end