Skip to content

Instantly share code, notes, and snippets.

@jerryclinesmith
Created November 22, 2013 16:44
Show Gist options
  • Save jerryclinesmith/7602977 to your computer and use it in GitHub Desktop.
Save jerryclinesmith/7602977 to your computer and use it in GitHub Desktop.
Chain a set of commands
module Chainer
STATUSES = [ STATUS_SUCCESS = :success, STATUS_FAIL = :fail ]
def self.execute(context, *steps)
steps.each do |step|
step.execute context
break if context.fail?
end
context
end
module Context
extend ActiveSupport::Concern
def status
@status || Chainer::STATUS_SUCCESS
end
def message
@message
end
def set_fail!(message)
self.message = message
self.status = Chainer::STATUS_FAIL
end
def success?
self.status == Chainer::STATUS_SUCCESS
end
def fail?
not success?
end
private
def status=(value)
@status = value
end
def message=(value)
@message = value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment