Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davetron5000/0f14fac096e938d58b72 to your computer and use it in GitHub Desktop.
Save davetron5000/0f14fac096e938d58b72 to your computer and use it in GitHub Desktop.
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