1# frozen_string_literal: true
2
3module Stepable
4  extend ActiveSupport::Concern
5
6  def steps
7    self.class._all_steps
8  end
9
10  def execute_steps
11    initial_result = {}
12
13    steps.inject(initial_result) do |previous_result, callback|
14      result = method(callback).call(previous_result)
15
16      if result[:status] != :success
17        result[:last_step] = callback
18
19        break result
20      end
21
22      result
23    end
24  end
25
26  class_methods do
27    def _all_steps
28      @_all_steps ||= []
29    end
30
31    def steps(*methods)
32      _all_steps.concat methods
33    end
34  end
35end
36