Skip to content

Instantly share code, notes, and snippets.

@grodowski
Created November 27, 2015 10:03
Show Gist options
  • Save grodowski/e503cb8e103167503232 to your computer and use it in GitHub Desktop.
Save grodowski/e503cb8e103167503232 to your computer and use it in GitHub Desktop.
codequest_pipes idea
# 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