Created
September 19, 2019 14:50
-
-
Save delbetu/0b72d8bd36e417db73fdbefcb690fbf7 to your computer and use it in GitHub Desktop.
Adding status to methods results
This file contains 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
class ResultWrapper | |
attr_reader :value, :success?, :error_messages | |
def failure? | |
!success? | |
end | |
end | |
class SomeController | |
def create | |
result = process(params[:id]) | |
if result.success? | |
render json: result.value.to_json | |
else | |
render json: { errors: result.error_messages }.to_json | |
end | |
end | |
def process(input) | |
return ResultWrapper.new(error_messages: ["Error processing #{input}"], success?: false) if input.nil? | |
ResultWrapper.new(value: input, success?: true) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment