define_exception 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,73 +1,20 @@
1
- # = Introduction
2
- #
3
1
  # Simple mixin that provides a way of defining custom exception classes
4
2
  # with default messages that dries up your code.
5
3
  #
6
- # = The Problem of Laborious Exception Class Definitions
7
- #
8
- # Typically you would write the following in your code to define a custom exception
9
- #
10
- # class MyError < RuntimeError; end #nodoc
11
- #
12
- # This seems simple enough until you have to raise it many times throughout your code
13
- # and have to provide an error message:
14
- #
15
- # raise( MyError, 'You shall not do what you just did' )
16
- #
17
- # It gets harder when you get tired of writing the same message to the <i>raise</i> command.
18
- # You can define a hash constant to keep track of common error messages:
19
- #
20
- # ERRORS = { :myerror => 'You shall not do what you just did', ... }
4
+ # = Class Methods
21
5
  #
22
- # and then reference it
6
+ # <tt>define_exception</tt>( <i>ExceptionName</i>, <i>'Default Error Message'</i> )
23
7
  #
24
- # raise( MyError, ERRORS[:myerror] )
8
+ # The exception name can either be a <i>string</i> or a <i>symbol</i>. If it is a <i>string</i>
9
+ # then the name will be as provided. In the <i>symbol</i> case if the symbol is mixed case the
10
+ # result exception name will be as provided. You also have the option of using underscores and having
11
+ # the method automatically generate the mixed case exception name:
25
12
  #
26
- # but this just doesn't seem very rubyish. You can also define the message when you write
27
- # the custom exception class:
13
+ # define_exception 'MyError', 'The Default Message'
14
+ # define_exception :AnotherError, 'Another Default Message'
15
+ # define_exception :worst_error, 'Worst Default Message'
28
16
  #
29
- # class MyError < RuntimeError
30
- # def message
31
- # 'You shall not do what you just did' )
32
- # end
33
- # end
34
- #
35
- # The problem is that this always uses this message even when overridden in <i>raise</i>
36
- #
37
- # raise( MyError, 'Use this instead' ) # <= 'You shall not do what you just did'
38
- #
39
- # The real solution for you class is to override the constructor:
40
- #
41
- # class MyError < RuntimeError
42
- # def intialize( message = nil )
43
- # super( message || 'Default Error Message' )
44
- # end
45
- # end
46
- #
47
- # raise MyError # <= 'Default Error Message'
48
- # raise MyError, 'Not the Default' # <= 'Not the Default'
49
- #
50
- # = A Better Way
51
- #
52
- # Though this accomplishes the goal it is laborious to write all this code especially
53
- # as the number of custom exception definitions grow. A better solution would be to make
54
- # a class method available that simply handles creating the exception for use automatically
55
- # thus drying up our code:
56
- #
57
- # class TestMe
58
- # define_exception 'MyError', 'The Default Message'
59
- # define_exception 'AnotherError', 'Another Default Message'
60
- # define_exception 'WorstError', 'Worst Default Message'
61
- # ...
62
- # def tester( args )
63
- # raise MyError unless ...
64
- # raise AnotherError unless ...
65
- # end
66
- #
67
- # def worst( args )
68
- # raise( WorstError, 'You really messed up' ) unless ...
69
- # end
70
- # end
17
+ # Any of the above methods will work.
71
18
  #
72
19
  # = Usage
73
20
  #
@@ -82,7 +29,7 @@
82
29
  # include DefineException
83
30
  #
84
31
  # class Test
85
- # define_exception 'TestMe', 'this is the default message'
32
+ # define_exception :test_me, 'this is the default message'
86
33
  #
87
34
  # def test
88
35
  # raise TestMe
@@ -105,7 +52,7 @@
105
52
  #
106
53
  # running the above example would correctly produce
107
54
  #
108
- # wes:~/Define-Exception> ruby test.rb
55
+ # wes:~/define_exception/examples> ruby sample.rb
109
56
  # this is the default message
110
57
  # test.rb:14:in `test2': You shall not do that again (Test::TestMe)
111
58
  # from test.rb:26
@@ -115,13 +62,17 @@
115
62
  # Author:: Wes Bailey, wes@verticalresponse.com
116
63
  # License:: Ruby License
117
64
 
65
+ class String
66
+ def lame_camel_case
67
+ /_/.match( self ) ? self.to_s.split( '_' ).inject( '' ) { |s,v| s << v.capitalize } : self
68
+ end
69
+ end
70
+
118
71
  module DefineException
119
72
  module ClassMethods
120
- def define_exception( exception_name, default_message = 'Application Error Occurred' )
121
- exception_name = exception_name.to_s.split( '_' ).inject( '' ) { |s,v| s << v.capitalize } if /\_/.match( exception_name.to_s )
122
-
73
+ def define_exception( exception, default_message = 'Application Error Occurred', ancestor = 'RuntimeError' )
123
74
  class_eval <<-EOD
124
- class #{exception_name} < RuntimeError
75
+ class #{exception.to_s.lame_camel_case} < #{ancestor.to_s.lame_camel_case}
125
76
  def initialize( message = nil )
126
77
  super( message || "#{default_message}" )
127
78
  end
@@ -132,7 +83,6 @@ module DefineException
132
83
 
133
84
  class << self
134
85
  def included( base )
135
- #base.extend( self )
136
86
  base.extend( ClassMethods )
137
87
  end
138
88
  end
@@ -6,34 +6,78 @@ require 'spec/test/unit'
6
6
 
7
7
  include DefineException
8
8
 
9
+ @@message = 'This is the default @@message'
10
+
9
11
  describe "Define Exception" do
10
- it "should enable a class to define a custom exception" do
11
- MESSAGE = 'This is the default message'
12
+ it "should enable a class to define a custom exception using a string" do
12
13
 
13
14
  class TestDefineException
14
- define_exception 'TestException', MESSAGE
15
+ define_exception 'TestException', @@message
15
16
 
16
17
  def test
17
18
  raise TestException
18
19
  end
19
20
  end
20
21
 
22
+ tde = TestDefineException.new
23
+
21
24
  lambda {
22
- tde = TestDefineException.new
23
25
  tde.test
24
- }.should raise_error( TestDefineException::TestException, MESSAGE )
26
+ }.should raise_error( TestDefineException::TestException, @@message )
27
+ end
25
28
 
29
+ it "should enable a class to define a custom exception using a symbol" do
26
30
  class AnotherTest
27
- define_exception :another_exception, MESSAGE
31
+ define_exception :AnotherException, @@message
28
32
 
29
33
  def test
30
34
  raise AnotherException
31
35
  end
32
36
  end
33
37
 
38
+ at = AnotherTest.new
39
+
34
40
  lambda {
35
- tde = AnotherTest.new
36
- tde.test
37
- }.should raise_error( AnotherTest::AnotherException, MESSAGE )
41
+ at.test
42
+ }.should raise_error( AnotherTest::AnotherException, @@message )
43
+ end
44
+
45
+ it "should enable a class to define a custom exception using a symbol converted to camel case" do
46
+ class YetAnotherTest
47
+ define_exception :yet_another_exception, @@message
48
+
49
+ def test
50
+ raise YetAnotherException
51
+ end
52
+ end
53
+
54
+ yat = YetAnotherTest.new
55
+
56
+ lambda {
57
+ yat.test
58
+ }.should raise_error( YetAnotherTest::YetAnotherException, @@message )
59
+ end
60
+
61
+ it "should enable a class to define a custom exception subclassing off of a different exception than RuntimeError" do
62
+ class AnyAncestorTest
63
+ define_exception 'MyArgException', @@message, 'ArgumentError'
64
+
65
+ def test
66
+ raise MyArgException
67
+ end
68
+ end
69
+
70
+ mae = AnyAncestorTest.new
71
+ lambda {
72
+ mae.test
73
+ }.should raise_error( AnyAncestorTest::MyArgException, @@message )
74
+
75
+ begin
76
+ mae.test
77
+ rescue AnyAncestorTest::MyArgException => e
78
+ e.is_a?( Exception ).should == true
79
+ e.is_a?( StandardError ).should == true
80
+ e.is_a?( ArgumentError ).should == true
81
+ end
38
82
  end
39
83
  end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: define_exception
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
5
11
  platform: ruby
6
12
  authors:
7
13
  - Wes Bailey
@@ -9,7 +15,7 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-05-24 00:00:00 -07:00
18
+ date: 2010-05-25 00:00:00 -07:00
13
19
  default_executable:
14
20
  dependencies: []
15
21
 
@@ -23,8 +29,11 @@ extra_rdoc_files: []
23
29
 
24
30
  files:
25
31
  - lib/define_exception.rb
32
+ - test/define_exception_spec.rb
26
33
  has_rdoc: true
27
34
  homepage: http://github.com/wbailey/define_exception
35
+ licenses: []
36
+
28
37
  post_install_message:
29
38
  rdoc_options:
30
39
  - --title
@@ -36,23 +45,29 @@ rdoc_options:
36
45
  require_paths:
37
46
  - lib
38
47
  required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
39
49
  requirements:
40
50
  - - ">="
41
51
  - !ruby/object:Gem::Version
52
+ hash: 3
53
+ segments:
54
+ - 0
42
55
  version: "0"
43
- version:
44
56
  required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
45
58
  requirements:
46
59
  - - ">="
47
60
  - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
48
64
  version: "0"
49
- version:
50
65
  requirements: []
51
66
 
52
67
  rubyforge_project:
53
- rubygems_version: 1.3.1
68
+ rubygems_version: 1.3.7
54
69
  signing_key:
55
- specification_version: 2
70
+ specification_version: 3
56
71
  summary: A simple way of defining exceptions for use in your ruby classes
57
72
  test_files:
58
73
  - test/define_exception_spec.rb