Module: Steppy::ClassMethods

Defined in:
lib/steppy/class_methods.rb

Overview

Steppy class methods that will be added to your included classes.

Instance Method Summary collapse

Instance Method Details

#step(method = nil, args = {}, &block) ⇒ Object Also known as: step_return



14
15
16
17
18
19
# File 'lib/steppy/class_methods.rb', line 14

def step(method = nil, args = {}, &block)
  steps.push(
    Steppy.parse_step(method: method, args: args, block: block)
  )
  self
end

#step_add_callback(type, block, key) ⇒ Object



64
65
66
67
68
# File 'lib/steppy/class_methods.rb', line 64

def step_add_callback(type, block, key)
  callback_key = key ? key.to_sym : :global
  callbacks = step_callbacks[type][callback_key] ||= []
  callbacks.push(block)
end

#step_after(key = nil, &block) ⇒ Object



56
57
58
# File 'lib/steppy/class_methods.rb', line 56

def step_after(key = nil, &block)
  step_add_callback(:after, block, key)
end

#step_before(key = nil, &block) ⇒ Object



60
61
62
# File 'lib/steppy/class_methods.rb', line 60

def step_before(key = nil, &block)
  step_add_callback(:before, block, key)
end

#step_callbacksObject



74
75
76
# File 'lib/steppy/class_methods.rb', line 74

def step_callbacks
  steppy_cache[:callbacks]
end

#step_if(condition, &block) ⇒ Object



23
24
25
26
# File 'lib/steppy/class_methods.rb', line 23

def step_if(condition, &block)
  steps.push(condition: condition, block: block)
  self
end

#step_if_else(condition_block, step_steps, args = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/steppy/class_methods.rb', line 38

def step_if_else(condition_block, step_steps, args = {})
  if_step, else_step = step_steps

  steps.push Steppy.parse_step(
    method: if_step,
    args: {
      if: condition_block,
    }.merge(args)
  )

  steps.push Steppy.parse_step(
    method: else_step,
    args: {
      unless: condition_block,
    }.merge(args)
  )
end

#step_rescue(exceptions = nil, &block) ⇒ Object



33
34
35
36
# File 'lib/steppy/class_methods.rb', line 33

def step_rescue(exceptions = nil, &block)
  steppy_cache[:rescues].push(exceptions: exceptions, block: block)
  self
end

#step_set(*sets) ⇒ Object



10
11
12
# File 'lib/steppy/class_methods.rb', line 10

def step_set(*sets)
  steppy_cache[:sets] += sets
end

#step_unless(condition, &block) ⇒ Object



28
29
30
31
# File 'lib/steppy/class_methods.rb', line 28

def step_unless(condition, &block)
  steps.push(condition: -> { !steppy_run_condition(condition) }, block: block)
  self
end

#steppy(&block) ⇒ Object



6
7
8
# File 'lib/steppy/class_methods.rb', line 6

def steppy(&block)
  steppy_cache[:block] = block
end

#steppy_cacheObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/steppy/class_methods.rb', line 78

def steppy_cache
  @steppy_cache ||= SteppyCache.new(
    steps: [],
    sets: [],
    rescues: [],
    callbacks: {
      before: {
        global: [],
      },
      after: {
        global: [],
      },
    }
  )
end

#stepsObject



70
71
72
# File 'lib/steppy/class_methods.rb', line 70

def steps
  steppy_cache[:steps]
end