2019-07-16 15:20:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Stepable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def steps
|
|
|
|
self.class._all_steps
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_steps
|
|
|
|
initial_result = {}
|
|
|
|
|
|
|
|
steps.inject(initial_result) do |previous_result, callback|
|
2019-10-18 14:06:21 -04:00
|
|
|
result = method(callback).call(previous_result)
|
2019-07-16 15:20:43 -04:00
|
|
|
|
2019-10-18 14:06:21 -04:00
|
|
|
if result[:status] != :success
|
|
|
|
result[:last_step] = callback
|
2019-07-16 15:20:43 -04:00
|
|
|
|
|
|
|
break result
|
|
|
|
end
|
|
|
|
|
2019-10-18 14:06:21 -04:00
|
|
|
result
|
2019-07-16 15:20:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def _all_steps
|
|
|
|
@_all_steps ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def steps(*methods)
|
|
|
|
_all_steps.concat methods
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|