Module: RubyBBCode::DebugBBTree

Included in:
BBTree, TagNode
Defined in:
lib/ruby-bbcode-to-md/debugging.rb

Overview

This module can be included in the BBTree and TagNode to give them debugging features

Instance Method Summary collapse

Instance Method Details

#count_child_nodes(hash = self) ⇒ Object

this blocky method counts how many children are in the TagNode.children, recursively walking the tree



45
46
47
48
49
50
51
# File 'lib/ruby-bbcode-to-md/debugging.rb', line 45

def count_child_nodes(hash = self)
  count = 0
  walk_tree(hash) do
    count += 1
  end
  count
end

#to_sObject

For BBTree, teh to_s method shows the count of the children plus a graphical depiction of the tree, and the relation of the nodes.

For TagNodes, the root-most tag is displayed, and the children are counted.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby-bbcode-to-md/debugging.rb', line 56

def to_s
  object_identifier = "#<#{self.class.to_s}:0x#{'%x' % (self.object_id << 1)}\n"
  close_object = ">\n"
  
  case self
  when RubyBBCode::BBTree
    object_identifier + "Children: #{count_child_nodes}\n" + self.to_v + close_object
  when RubyBBCode::TagNode   # when inspecting TagNodes, it's better not to show the tree display
    if self[:is_tag]
      object_identifier + "Tag:  #{self[:tag].to_s}, Children: #{count_child_nodes}\n" + close_object
    else
      object_identifier + '"' + self[:text].to_s + "\"\n" + close_object
    end
  end
end

#to_vObject

For Debugging/ visualization purposes. This can be used to render the #nodes array in a pretty manor, showing the hirarchy.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-bbcode-to-md/debugging.rb', line 25

def to_v
  tree = self
  visual_string = ''
  
  walk_tree(tree) do |node, depth|
    indentation = '  ' * depth
    case node[:is_tag]
    when true
      visual_string += "#{indentation}" + node[:tag].to_s + "\n"
    when false
      visual_string += "#{indentation}\"#{node[:text]}\"\n"
    end
  end
  
  visual_string
end