Created
February 14, 2017 21:55
-
-
Save blvrd/973a4538a63413aed029c80996e8a522 to your computer and use it in GitHub Desktop.
Flow
This file contains hidden or 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
# Every block should catch errors and add them to the instance of CheckoutFlow | |
# | |
# CheckoutFlow.new.execute do | |
# # ... | |
# end.then do | |
# # ... | |
# end | |
class Flow | |
def initialize | |
@error = nil | |
@_values = {} | |
end | |
attr_reader :error, :_values | |
def execute(fail_silently: false, &block) | |
if block_given? | |
unless @error | |
yield | |
end | |
self | |
end | |
rescue => e | |
unless fail_silently | |
@error = e | |
end | |
Rails.logger.error("Error: #{e.inspect}") | |
Appsignal.set_error(e) | |
self | |
end | |
alias_method :then, :execute | |
def add_value(hash) | |
@_values = @_values.merge(hash) | |
end | |
def get_value(key) | |
value = @_values[key.to_sym] | |
unless value | |
raise MissingValueError, "The flow doesn't have the value: #{key}" | |
end | |
value | |
end | |
class MissingValueError < StandardError; end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment