Class: Teek::UI::TreeInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/teek/ui/tree_inspector.rb

Overview

Build-phase debug tooling: a readable ASCII tree of a Document's current shape, and (opt-in) a step-by-step trace of how it got there. Deliberately its own class, not methods on Document/Node - those stay focused on constructing/indexing/parent-tracking the retained tree; this is purely an outside observer of it, built the same way an app author's own tooling would be (see Document#subscribe).

Defined Under Namespace

Classes: Event

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, trace: false) ⇒ TreeInspector

Returns a new instance of TreeInspector.

Parameters:

  • document (Document)
  • trace (Boolean) (defaults to: false)

    subscribe to +document+'s own internal build-event hook immediately, recording every stack push/pop and node append from this point on - see #log. false (the default) costs nothing beyond this object's own existence: document never learns whether a TreeInspector is watching or not, on or off.



40
41
42
43
44
# File 'lib/teek/ui/tree_inspector.rb', line 40

def initialize(document, trace: false)
  @document = document
  @log = []
  subscribe! if trace
end

Instance Attribute Details

#logArray<Event> (readonly)

Returns every push/pop/append recorded so far, in order - always empty unless constructed with trace: true.

Returns:

  • (Array<Event>)

    every push/pop/append recorded so far, in order - always empty unless constructed with trace: true



73
74
75
# File 'lib/teek/ui/tree_inspector.rb', line 73

def log
  @log
end

Instance Method Details

This method returns an undefined value.

#to_s, straight to stdout.



67
68
69
# File 'lib/teek/ui/tree_inspector.rb', line 67

def print_tree
  puts to_s
end

#to_sString

A readable ASCII tree of the document's CURRENT shape - type, name, and (where present) +opts+/+opts+, box-drawn by nesting. Reflects whatever the tree looks like right now; call it again after more building to see the updated shape.

Examples:

root
└─ column
   ├─ label "Title"
   ├─ row
     ├─ button "OK"
     └─ button "Cancel"
   └─ label "Footer"

Returns:

  • (String)


59
60
61
62
63
# File 'lib/teek/ui/tree_inspector.rb', line 59

def to_s
  lines = [@document.root.display_name]
  append_tree_lines(@document.root, '', lines)
  lines.join("\n")
end