Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1.  
  2. # Sample controller
  3. def create
  4. ctx = Pipes::Context.new(…..)
  5. flow.call(ctx)
  6. render json: ctx.project, status: :ok
  7. # no need for handling specific exceptions of multiple types
  8. resuce Pipes::Error => e
  9. render json: {errors: e.message}
  10. end
  11.  
  12. class SaveProject
  13. def call
  14. ctx.project.save!
  15. end
  16.  
  17. rescue RecordInvalid => e
  18. # hide the internals of record invalid for the public pipes api
  19. ctx.fail(e, e.record.errors.full_messages)
  20. end
  21. end
  22.  
  23. class Pipes::Context
  24. attr_reader :error
  25.  
  26. def fail(error)
  27. @error = error
  28. fail Pipes::Error
  29. end
  30.  
  31. def success?
  32. @error.nil?
  33.  
  34. def error?
  35. !success?
  36. end
  37. end
  38.  
  39. # May not be the best idea, but
  40. # returning a boolean and then accessing ctx.error instead of raising
  41. # an exception for controlling the flow sounds good to me
  42. module Pipe
  43. def method_missing
  44.  
  45. rescue Pipes::Error
  46.  
  47. end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement