Created
June 17, 2015 21:27
-
-
Save davetron5000/0f14fac096e938d58b72 to your computer and use it in GitHub Desktop.
This file contains 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
service = MyService.new | |
service.doit("foo").on_done do |result| | |
# this is the happy path--doit did whatever it was supposed to | |
end.on_still_in_progress do | |
# this could be an alternate path, e.g. the request is still in progress | |
end.on_error do |exception| | |
# This could be the error path, where you get an exception passed | |
end | |
class MyService | |
def doit(foo) | |
# .... | |
if completed | |
Done.new(result) | |
else | |
InProgress.new | |
end | |
rescue => ex | |
OnError.new(ex) | |
end | |
class Callbacks | |
def on_done(*); self; end | |
def still_in_progress(*); self; end | |
def on_error(*); self; end | |
end | |
class Done < Callbacks | |
def initialize(result) | |
@result = result | |
end | |
def on_done(&block) | |
block.call(@result) | |
super | |
end | |
end | |
# ... and so forth | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment