Last active
August 29, 2015 14:02
-
-
Save kwstannard/dcc4fbcf7c745f5e73b2 to your computer and use it in GitHub Desktop.
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
class OnCodeCaller | |
def self.call(code, needer) | |
if needer.respond_to?("on_#{code}") | |
needer.send("on_#{code}") | |
elsif needer.respond_to?("on_#{code / 100}XX") | |
needer.send("on_#{code / 100}XX") | |
else | |
needer.on_failure | |
end | |
end | |
end | |
class Foo | |
def bar(code) | |
CodeCaller.call(code, self) | |
end | |
def on_422 | |
raise 'specific error called if defined' | |
end | |
def on_4XX | |
raise 'generic server error if no specific error code defined' | |
end | |
def on_failure | |
{} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment