Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Gracefully
class Gracefully
class << self
alias_method :handle, :new
end
attr_reader :error, :value
def initialize
@value = nil
@error = nil
begin
@value = yield
rescue => e
@error = e
end
end
def success?
error.nil?
end
def on_success
return self unless success?
result = yield(@value)
return result if result.is_a?(Gracefully)
Gracefully.handle { result }
end
def on_failure
return self if success?
result = yield(@error)
return result if result.is_a?(Gracefully)
Gracefully.handle { result }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment