Skip to content

Instantly share code, notes, and snippets.

@blvrd
Created February 14, 2017 21:55
Show Gist options
  • Save blvrd/973a4538a63413aed029c80996e8a522 to your computer and use it in GitHub Desktop.
Save blvrd/973a4538a63413aed029c80996e8a522 to your computer and use it in GitHub Desktop.
Flow
# 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