Created
November 27, 2015 10:03
-
-
Save grodowski/e503cb8e103167503232 to your computer and use it in GitHub Desktop.
codequest_pipes idea
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
# Sample controller | |
def create | |
ctx = Pipes::Context.new(…..) | |
flow.call(ctx) | |
render json: ctx.project, status: :ok | |
# no need for handling specific exceptions of multiple types | |
resuce Pipes::Error => e | |
render json: {errors: e.message} | |
end | |
class SaveProject | |
def call | |
ctx.project.save! | |
end | |
rescue RecordInvalid => e | |
# hide the internals of record invalid for the public pipes api | |
ctx.fail(e, e.record.errors.full_messages) | |
end | |
end | |
class Pipes::Context | |
attr_reader :error | |
def fail(error) | |
@error = error | |
fail Pipes::Error | |
end | |
def success? | |
@error.nil? | |
def error? | |
!success? | |
end | |
end | |
# May not be the best idea, but | |
# returning a boolean and then accessing ctx.error instead of raising | |
# an exception for controlling the flow sounds good to me | |
module Pipe | |
def method_missing | |
rescue Pipes::Error | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment