Last active
November 5, 2024 18:29
-
-
Save Hasstrup/4b6a604c0df669cd83460c5251ac7726 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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