Skip to content

Instantly share code, notes, and snippets.

@davetron5000
Created September 7, 2014 21:39
Show Gist options
  • Save davetron5000/c8861340e78a3b10a79f to your computer and use it in GitHub Desktop.
Save davetron5000/c8861340e78a3b10a79f to your computer and use it in GitHub Desktop.
Custom Control Structure
# instead of designing a class to return a result object that we
# must interrogate, we could design our class to have a custom
# control structure to make it easy to see the alternatives
# that must be handled
PaymentProcessor.new.charge_money(credit_card,amount).on_success do |transaction_id|
# happy path
end.on_expired_card do
# special message to get them to update their card
end.on_decline do |decline_message|
# general decline
end.on_exception do |exception|
# something went wrong and we could try again
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment