Class: BracketNotation::View::Tree
- Inherits:
-
Object
- Object
- BracketNotation::View::Tree
- Defined in:
- lib/bracket_notation/views/tree.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#color_bg ⇒ Object
Returns the value of attribute color_bg.
-
#color_branch ⇒ Object
Returns the value of attribute color_branch.
-
#color_fg ⇒ Object
Returns the value of attribute color_fg.
-
#color_leaf ⇒ Object
Returns the value of attribute color_leaf.
-
#color_line ⇒ Object
Returns the value of attribute color_line.
-
#font_name ⇒ Object
Returns the value of attribute font_name.
-
#font_point_size ⇒ Object
Returns the value of attribute font_point_size.
-
#input ⇒ Object
Returns the value of attribute input.
-
#node_h_margin ⇒ Object
Returns the value of attribute node_h_margin.
-
#node_padding ⇒ Object
Returns the value of attribute node_padding.
-
#node_v_margin ⇒ Object
Returns the value of attribute node_v_margin.
-
#root ⇒ Object
Returns the value of attribute root.
-
#tree_padding ⇒ Object
Returns the value of attribute tree_padding.
Class Method Summary collapse
-
.prune ⇒ Object
Throws :prune, which is caught by #traverse, signaling that method to cease visiting nodes on the current branch.
Instance Method Summary collapse
-
#compute_layout ⇒ Object
Computes the node tree layout, setting the correct origin and dimensions for each node.
-
#initialize(input) ⇒ Tree
constructor
A new instance of Tree.
- #inspect ⇒ Object (also: #to_s)
- #pretty_print ⇒ Object
-
#traverse(options = {}, &block) ⇒ Object
Traverse the tree, passing each visited node and the current depth to the given block.
Constructor Details
#initialize(input) ⇒ Tree
Returns a new instance of Tree.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bracket_notation/views/tree.rb', line 40 def initialize(input) @input = input @font_name = "Helvetica" @font_point_size = 40 @tree_padding = 20 @node_h_margin = 50 @node_v_margin = 30 @node_padding = 10 @color_bg = "white" @color_fg = "black" @color_line = "red" @color_branch = "blue" @color_leaf = "green" end |
Instance Attribute Details
#color_bg ⇒ Object
Returns the value of attribute color_bg.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def color_bg @color_bg end |
#color_branch ⇒ Object
Returns the value of attribute color_branch.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def color_branch @color_branch end |
#color_fg ⇒ Object
Returns the value of attribute color_fg.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def color_fg @color_fg end |
#color_leaf ⇒ Object
Returns the value of attribute color_leaf.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def color_leaf @color_leaf end |
#color_line ⇒ Object
Returns the value of attribute color_line.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def color_line @color_line end |
#font_name ⇒ Object
Returns the value of attribute font_name.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def font_name @font_name end |
#font_point_size ⇒ Object
Returns the value of attribute font_point_size.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def font_point_size @font_point_size end |
#input ⇒ Object
Returns the value of attribute input.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def input @input end |
#node_h_margin ⇒ Object
Returns the value of attribute node_h_margin.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def node_h_margin @node_h_margin end |
#node_padding ⇒ Object
Returns the value of attribute node_padding.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def node_padding @node_padding end |
#node_v_margin ⇒ Object
Returns the value of attribute node_v_margin.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def node_v_margin @node_v_margin end |
#root ⇒ Object
Returns the value of attribute root.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def root @root end |
#tree_padding ⇒ Object
Returns the value of attribute tree_padding.
32 33 34 |
# File 'lib/bracket_notation/views/tree.rb', line 32 def tree_padding @tree_padding end |
Class Method Details
.prune ⇒ Object
Throws :prune, which is caught by #traverse, signaling that method to cease visiting nodes on the current branch.
36 37 38 |
# File 'lib/bracket_notation/views/tree.rb', line 36 def self.prune throw(:prune) end |
Instance Method Details
#compute_layout ⇒ Object
Computes the node tree layout, setting the correct origin and dimensions for each node.
97 98 99 100 101 102 103 104 105 |
# File 'lib/bracket_notation/views/tree.rb', line 97 def compute_layout layout_nodes old_root_origin_x = @root.rect.origin.x new_root_origin_x = ((@root.subtree_size.width + (@tree_padding * 2)) / 2) - (@root.rect.size.width / 2) delta = new_root_origin_x - old_root_origin_x traverse {|node, depth| node.rect = BracketNotation::Geometry::Rect.new(node.rect.origin.point_by_adding_to_x(delta), node.rect.size) } end |
#inspect ⇒ Object Also known as: to_s
107 108 109 110 111 |
# File 'lib/bracket_notation/views/tree.rb', line 107 def inspect leaf_content = [] traverse {|node, depth| leaf_content << node.content if node.kind_of? Leaf } return "#{@input} >> \"#{leaf_content.join(" ")}\"" end |
#pretty_print ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/bracket_notation/views/tree.rb', line 115 def pretty_print traverse do |node, depth| depth.times { print "--" } print " " if depth > 0 puts node.to_s end end |
#traverse(options = {}, &block) ⇒ Object
Traverse the tree, passing each visited node and the current depth to the given block.
Options are:
-
:depth
- The starting value of the depth counter. The default is0
. -
:order
- The traversal order to follow. Allowed values are::preorder
(or:pre
),:postorder
(or:post
),:breadthfirst
(or:breadth
). The default is:preorder
. -
:root
- The root node of the subtree to be traversed. The default is the root node of the entire tree.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/bracket_notation/views/tree.rb', line 67 def traverse( = {}, &block) [:order] ||= :preorder [:depth] ||= 0 [:root] ||= @root return if @root.nil? if [:breadth, :breadthfirst].include? [:order] node_queue = [[:root]] while node = node_queue.shift yield node, node.ancestors.length node_queue += node.children end else catch(:prune) do case [:order] when :pre, :preorder yield [:root], [:depth] [:root].children.each {|child| traverse({:order => :pre, :root => child, :depth => [:depth] + 1}, &block) } when :post, :postorder [:root].children.each {|child| traverse({:order => :post, :root => child, :depth => [:depth] + 1}, &block) } yield [:root], [:depth] end end end end |