Skip to content

Instantly share code, notes, and snippets.

@delbetu
Created September 19, 2019 14:50
Show Gist options
  • Save delbetu/0b72d8bd36e417db73fdbefcb690fbf7 to your computer and use it in GitHub Desktop.
Save delbetu/0b72d8bd36e417db73fdbefcb690fbf7 to your computer and use it in GitHub Desktop.
Adding status to methods results
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