Class: PaintbrushSupport::ElementTree
- Inherits:
-
Object
- Object
- PaintbrushSupport::ElementTree
- Defined in:
- lib/paintbrush_support/element_tree.rb
Overview
A tree of BoundedColorElement objects. Used to build a full tree of colorized substrings in order to allow discovery of parent substrings and use their color code to restore to when the substring is terminated. Allows deeply-nested colorized strings.
Instance Attribute Summary collapse
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Instance Method Summary collapse
- #build_tree(boundary_end:, root: @tree, elements: bounded_color_elements, boundary_start: 0) ⇒ Object
-
#initialize(bounded_color_elements:) ⇒ ElementTree
constructor
A new instance of ElementTree.
Constructor Details
#initialize(bounded_color_elements:) ⇒ ElementTree
Returns a new instance of ElementTree.
10 11 12 13 14 |
# File 'lib/paintbrush_support/element_tree.rb', line 10 def initialize(bounded_color_elements:) @bounded_color_elements = bounded_color_elements @tree = { node: nil, children: [] } build_tree(boundary_end: bounded_color_elements.map(&:close_index).max) end |
Instance Attribute Details
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
8 9 10 |
# File 'lib/paintbrush_support/element_tree.rb', line 8 def tree @tree end |
Instance Method Details
#build_tree(boundary_end:, root: @tree, elements: bounded_color_elements, boundary_start: 0) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/paintbrush_support/element_tree.rb', line 16 def build_tree(boundary_end:, root: @tree, elements: bounded_color_elements, boundary_start: 0) root_nodes, non_root_nodes = partitioned_elements(elements, boundary_start, boundary_end) root_nodes.each do |node| root[:children] << child_node(node) build_tree( root: root[:children].last, elements: non_root_nodes, boundary_start: node.open_index, boundary_end: node.close_index ) end end |