Last active
August 29, 2015 14:09
-
-
Save tastycode/3da4984004f4b4a483c9 to your computer and use it in GitHub Desktop.
Got a whole bunch of `fails` in your ruby codebase?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FailClassInference | |
def fail(*args) | |
return super unless args.first.is_a?(String) | |
super(infer_exception_class, *args) | |
end | |
def raise(*args) | |
return super unless args.first.is_a?(String) | |
super(infer_exception_class, *args) | |
end | |
def infer_exception_class | |
klass = self.is_a?(Class) ? self : self.class | |
expected_error_klass = "StringError" | |
error_klass = if klass.const_defined?(expected_error_klass) | |
klass.const_get(expected_error_klass) | |
else | |
klass.const_set(expected_error_klass, Class.new(RuntimeError)) | |
end | |
end | |
end | |
Object.include(FailClassInference) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment