Module: Trailblazer::Developer::Wtf

Defined in:
lib/trailblazer/developer/wtf.rb

Defined Under Namespace

Modules: String

Constant Summary collapse

COLOR_MAP =
{ pass: :green, fail: :brown }
SIGNALS_MAP =
{
  'Trailblazer::Activity::Right': :pass,
  'Trailblazer::Activity::FastTrack::PassFast': :pass,

  'Trailblazer::Activity::Left': :fail,
  'Trailblazer::Activity::FastTrack::FailFast': :fail,
}

Class Method Summary collapse

Class Method Details

.fmt(line, style) ⇒ Object



66
67
68
69
# File 'lib/trailblazer/developer/wtf.rb', line 66

def fmt(line, style)
  return line unless style
  String.send(style, line)
end

.invoke(activity, ctx, flow_options, *args) ⇒ Object

Run activity with tracing enabled and inject a mutable Stack instance. This allows to display the trace even when an exception happened



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/trailblazer/developer/wtf.rb', line 27

def invoke(activity, (ctx, flow_options), *args)
  flow_options ||= {} # Ruby sucks.

  # this instance gets mutated with every step. unfortunately, there is
  # no other way in Ruby to keep the trace even when an exception was thrown.
  stack = Trace::Stack.new

  _returned_stack, *returned = Trace.invoke(
    activity,
    [
      ctx,
      flow_options.merge(stack: stack)
    ],
    *args
  )

  returned
ensure
  puts Trace::Present.(
    stack,
    renderer: method(:renderer),
    color_map: COLOR_MAP.merge( flow_options[:color_map] || {} )
  )
end

.renderer(task_node:, position:, tree:) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/trailblazer/developer/wtf.rb', line 52

def renderer(task_node:, position:, tree:)
  name, level, output, color_map = task_node.values_at(:name, :level, :output, :color_map)

  if output.nil? && tree[position.next].nil? # i.e. when exception raised
    return [ level, %{#{fmt(fmt(name, :red), :bold)}} ]
  end

  if output.nil? # i.e. on entry/exit point of activity
    return [ level, %{#{name}} ]
  end

  [ level, %{#{fmt( name, color_map[ signal_of(output.data) ] )}} ]
end

.signal_of(entity_output) ⇒ Object



71
72
73
74
# File 'lib/trailblazer/developer/wtf.rb', line 71

def signal_of(entity_output)
  entity_klass = entity_output.is_a?(Class) ? entity_output : entity_output.class
  SIGNALS_MAP[entity_klass.name.to_sym]
end