Class: BracketNotation::View::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/bracket_notation/views/tree.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_bgObject

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_branchObject

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_fgObject

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_leafObject

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_lineObject

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_nameObject

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_sizeObject

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

#inputObject

Returns the value of attribute input.



32
33
34
# File 'lib/bracket_notation/views/tree.rb', line 32

def input
  @input
end

#node_h_marginObject

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_paddingObject

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_marginObject

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

#rootObject

Returns the value of attribute root.



32
33
34
# File 'lib/bracket_notation/views/tree.rb', line 32

def root
  @root
end

#tree_paddingObject

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

.pruneObject

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_layoutObject

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

#inspectObject 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_printObject



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 is 0.

  • :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(options = {}, &block)
  options[:order] ||= :preorder
  options[:depth] ||= 0
  options[:root] ||= @root
  
  return if @root.nil?
  
  if [:breadth, :breadthfirst].include? options[:order]
    node_queue = [options[:root]]
    
    while node = node_queue.shift
      yield node, node.ancestors.length
      node_queue += node.children
    end
  else
    catch(:prune) do
      case options[:order]
        when :pre, :preorder
          yield options[:root], options[:depth]
          options[:root].children.each {|child| traverse({:order => :pre, :root => child, :depth => options[:depth] + 1}, &block) }
        when :post, :postorder
          options[:root].children.each {|child| traverse({:order => :post, :root => child, :depth => options[:depth] + 1}, &block) }
          yield options[:root], options[:depth]
      end
    end
  end
end