Skip to content

Instantly share code, notes, and snippets.

@katpadi
Created April 5, 2018 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katpadi/9aac974a760fc3f5f3a287bb75804de6 to your computer and use it in GitHub Desktop.
Save katpadi/9aac974a760fc3f5f3a287bb75804de6 to your computer and use it in GitHub Desktop.
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