Module: Chaintown::Steps

Included in:
Step
Defined in:
lib/chaintown/steps.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

When module is extended we want to make steps and failed steps variabled of specific class type, so every class will have its own list of steps



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chaintown/steps.rb', line 25

def self.extended(base)
  base.class_eval do
    attr_writer :steps, :failed_steps

    define_singleton_method(:steps) do
      instance_variable_get(:@steps) || instance_variable_set(:@steps, [])
    end

    define_singleton_method(:failed_steps) do
      instance_variable_get(:@failed_steps) || instance_variable_set(:@failed_steps, [])
    end
  end
end

.included(base) ⇒ Object

When module is included we want to make steps and failed_steps instance variables



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/chaintown/steps.rb', line 8

def self.included(base)
  base.class_eval do
    attr_writer :steps, :failed_steps

    define_method(:steps) do
      instance_variable_get(:@steps) || instance_variable_set(:@steps, [])
    end

    define_method(:failed_steps) do
      instance_variable_get(:@failed_steps) || instance_variable_set(:@failed_steps, [])
    end
  end
end

Instance Method Details

#failed_step(step_handler, **params, &block) ⇒ Object

DSL method to add new step called when process will fail



45
46
47
# File 'lib/chaintown/steps.rb', line 45

def failed_step(step_handler, **params, &block)
  failed_steps << init_step(step_handler, params, &block)
end

#inherited(subclass) ⇒ Object

Callback, assure that we add steps from parent class



50
51
52
53
54
55
# File 'lib/chaintown/steps.rb', line 50

def inherited(subclass)
  [:steps, :failed_steps].each do |inheritable_attribute|
    instance_var = "@#{inheritable_attribute}"
    subclass.instance_variable_set(instance_var, instance_variable_get(instance_var).dup || [])
  end
end

#step(step_handler, **params, &block) ⇒ Object

DLS method to add new step to the list



40
41
42
# File 'lib/chaintown/steps.rb', line 40

def step(step_handler, **params, &block)
  steps << init_step(step_handler, params, &block)
end