Module: CrackPipe::Action::Exec

Defined in:
lib/crack_pipe/action/exec.rb

Class Method Summary collapse

Class Method Details

.action(action, context, track = :default) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/crack_pipe/action/exec.rb', line 13

def action(action, context, track = :default)
  action.class.steps.each_with_object([]) do |s, results|
    next unless track == s.track
    results!(results, action, s, context).last.tap do |r|
      action.after_flow_control(r)
      context = r[:context]
      track = r[:next]
      return results if r[:signal] == :halt
    end
  end
end

.call(action, context, track = :default) ⇒ Object



9
10
11
# File 'lib/crack_pipe/action/exec.rb', line 9

def call(action, context, track = :default)
  Result.new(action(action, context, track))
end

.flow_control_hash(action, step, context, output) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/crack_pipe/action/exec.rb', line 25

def flow_control_hash(action, step, context, output)
  success = success_with_step?(action, step, output)

  {
    exec: step.exec,
    track: step.track,
    next: success ? step.track : :fail,
    context: context.dup
  }.merge(flow_control_with_output(output, success))
end

.flow_control_with_output(output, success) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crack_pipe/action/exec.rb', line 36

def flow_control_with_output(output, success)
  case output
  when Signal
    {
      signal: output.type,
      output: output.value,
      success: output.success.nil? ? success : output.success
    }
  else
    { output: output, success: success }
  end
end

.halt(output, success = nil) ⇒ Object



49
50
51
# File 'lib/crack_pipe/action/exec.rb', line 49

def halt(output, success = nil)
  throw(:signal, Signal.new(:halt, output, success))
end

.step(action, step, context) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/crack_pipe/action/exec.rb', line 53

def step(action, step, context)
  kwargs = kwargs_with_context(action, context)

  output = catch(:signal) do
    if (e = step.exec).is_a?(Symbol)
      action.public_send(e, context, **kwargs)
    else
      e.call(context, **kwargs)
    end
  end

  action.after_step(output)
end

.success_with_step?(action, step, output) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/crack_pipe/action/exec.rb', line 67

def success_with_step?(action, step, output)
  step.always_pass? || step.track != :fail && !action.failure?(output)
end