Module: Trailblazer::Developer::Trace::Present

Defined in:
lib/trailblazer/developer/trace/present.rb

Overview

DISCUSS: rename to Debugger?

Class Method Summary collapse

Class Method Details

.call(stack, render_method: method(:render), **options, &block) ⇒ Object

Entry point for rendering a Stack. Used in ‘#wtf?`.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/trailblazer/developer/trace/present.rb', line 25

def call(stack, render_method: method(:render), **options, &block)
  deprecate_node_options!(**options) # TODO: remove in 0.2.0.

  # Build a generic array of {Trace::Node}s.
  trace_nodes = Trace.build_nodes(stack.to_a)

  # The top activity doesn't have an ID, hence we need to compute a default label.
  top_activity_trace_node = trace_nodes[0]

  build_options = {
    node_options: {
      # we can pass particular label "hints".
      top_activity_trace_node => {
        # label: %{#{top_activity_trace_node.task.superclass} (anonymous)},
        label: top_activity_trace_node.task.inspect,
      },
    }
  }

  build_options = build_options.merge(options) # since we only have {:node_options} in {build_options}, we can safely merge here.

  # specific rendering.
  options_from_block = block_given? ? block.call(trace_nodes: trace_nodes, stack: stack, **build_options) : {}

  build_options = merge_local_options(options_from_block, build_options)

  # currently, we agree on using a Debugger::Node list as the presentation data structure.
  debugger_trace = Debugger::Trace.build(stack, trace_nodes, **build_options)

  return render_method.(debugger_trace: debugger_trace, **build_options)
end

.default_renderer(debugger_node:) ⇒ Object



9
10
11
# File 'lib/trailblazer/developer/trace/present.rb', line 9

def default_renderer(debugger_node:, **) # DISCUSS: for compatibility, should we pass {:task_node} here, too?
  [debugger_node.level, debugger_node.label]
end

.deprecate_node_options!(node_options: nil) ⇒ Object

TODO: remove in 0.2.0.



57
58
59
60
61
62
# File 'lib/trailblazer/developer/trace/present.rb', line 57

def deprecate_node_options!(node_options: nil, **) # TODO: remove in 0.2.0.
  return unless node_options

  raise "[Trailblazer] The `:node_options` option for `Trace::Present` is deprecated.
  Please use the block style as described here: https://trailblazer.to/2.1/docs/internals.html#internals-developer-trace-present"
end

.merge_local_options(options, local_options) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/trailblazer/developer/trace/present.rb', line 65

def merge_local_options(options, local_options)
  merged_hash = options.collect do |key, value|
    [
      key,
      value.is_a?(Hash) ? local_options.fetch(key, {}).merge(value) : value # options are winning over local_options[key]
    ]
  end.to_h

  local_options.merge(merged_hash)
end

.render(debugger_trace:, renderer: method(:default_renderer), **options_for_renderer) ⇒ Object

Returns the console output string.



15
16
17
18
19
20
21
# File 'lib/trailblazer/developer/trace/present.rb', line 15

def render(debugger_trace:, renderer: method(:default_renderer), **options_for_renderer)
  nodes = debugger_trace.to_a.collect do |debugger_node|
    renderer.(debugger_node: debugger_node, debugger_trace: debugger_trace, **options_for_renderer)
  end

  Hirb::Console.format_output(nodes, class: :tree, type: :directory, multi_line_nodes: true)
end