Module: Trailblazer::Developer::Wtf

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

Defined Under Namespace

Modules: Renderer

Class Method Summary collapse

Class Method Details

.arguments_for_trace(activity, ctx, original_flow_options, **circuit_options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/trailblazer/developer/wtf.rb', line 35

def arguments_for_trace(activity, (ctx, original_flow_options), **circuit_options)
  default_flow_options = {
    # 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,

    input_data_collector: method(:trace_input_data_collector),
    output_data_collector: method(:trace_output_data_collector),
  }

  # Merge default options with flow_options as an order of precedence
  flow_options = { **default_flow_options, **Hash( original_flow_options ) }

  # Normalize `focus_on` param to
  #   1. Wrap step and variable names into an array if not already
  flow_options[:focus_on] = {
    steps: Array( flow_options.dig(:focus_on, :steps) ),
    variables: Array( flow_options.dig(:focus_on, :variables) ),
  }

  [activity, [ ctx, flow_options ], circuit_options]
end

.capture_variables?(step_name:, focus_on:) ⇒ Boolean

private

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/trailblazer/developer/wtf.rb', line 83

def capture_variables?(step_name:, focus_on:, **)
  return true if focus_on[:steps].include?(step_name)                 # For given step
  return true if focus_on[:steps].empty? && focus_on[:variables].any? # For selected vars but all steps

  false
end

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

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/trailblazer/developer/wtf.rb', line 17

def invoke(activity, (ctx, flow_options), **circuit_options)
  activity, (ctx, flow_options), circuit_options = Wtf.arguments_for_trace(
    activity, [ctx, flow_options], **circuit_options
  )

  _returned_stack, signal, (ctx, flow_options) = Trace.invoke(
    activity, [ctx, flow_options], **circuit_options
  )

  return signal, [ctx, flow_options], circuit_options
ensure
  puts Trace::Present.(
    flow_options[:stack],
    renderer: Wtf::Renderer,
    color_map: Wtf::Renderer::DEFAULT_COLOR_MAP.merge( flow_options[:color_map] || {} ),
  )
end

.trace_input_data_collector(wrap_config, ctx, flow_options, circuit_options) ⇒ Object

Overring default input and output data collectors to collect/capture

1. inspect of focusable variables for given focusable step
2. Or inspect of focused variables for all steps


61
62
63
64
65
66
67
68
69
# File 'lib/trailblazer/developer/wtf.rb', line 61

def trace_input_data_collector(wrap_config, (ctx, flow_options), circuit_options)
  data = Trace.default_input_data_collector(wrap_config, [ctx, flow_options], circuit_options)

  if Wtf.capture_variables?(step_name: data[:task_name], **flow_options)
    data[:focused_variables] = Trace::Focusable.capture_variables_from(ctx, **flow_options)
  end

  data
end

.trace_output_data_collector(wrap_config, ctx, flow_options, circuit_options) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/trailblazer/developer/wtf.rb', line 71

def trace_output_data_collector(wrap_config, (ctx, flow_options), circuit_options)
  data  = Trace.default_output_data_collector(wrap_config, [ctx, flow_options], circuit_options)
  input = flow_options[:stack].top

  if Wtf.capture_variables?(step_name: input.data[:task_name], **flow_options)
    data[:focused_variables] = Trace::Focusable.capture_variables_from(ctx, **flow_options)
  end

  data
end