Skip to content

Instantly share code, notes, and snippets.

@Hasstrup
Last active November 5, 2024 18:29
Show Gist options
  • Save Hasstrup/4b6a604c0df669cd83460c5251ac7726 to your computer and use it in GitHub Desktop.
Save Hasstrup/4b6a604c0df669cd83460c5251ac7726 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# The Context class is used to encapsulate the result of a service operation,
# holding information about success, errors, messages, and any relevant payload.
class Context
attr_reader :success, :errors, :messages, :payload
def initialize
@success = true
@errors = []
@messages = []
end
def fail!(error:)
@success = false
@errors = [error]
self
end
def succeed(payload = nil)
clear_errors
@success = true
@payload = payload
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment