Module: TreeNode

Includes:
Enumerable
Included in:
Apotomo::Widget
Defined in:
lib/apotomo/tree_node.rb

Overview

stolen from … ? couldn’t find the original lib on the net. TODO: insert copyright notice!

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenHashObject (readonly)

Returns the value of attribute childrenHash.



7
8
9
# File 'lib/apotomo/tree_node.rb', line 7

def childrenHash
  @childrenHash
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/apotomo/tree_node.rb', line 7

def name
  @name
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/apotomo/tree_node.rb', line 8

def parent
  @parent
end

Class Method Details

.included(base) ⇒ Object



10
11
12
# File 'lib/apotomo/tree_node.rb', line 10

def self.included(base)
  base.after_initialize :initialize_tree_node
end

Instance Method Details

#<<(child) ⇒ Object

Convenience synonym for Tree#add method. This method allows a convenient method to add children hierarchies in the tree. E.g. root << child << grand_child



31
32
33
# File 'lib/apotomo/tree_node.rb', line 31

def <<(child)
  add(child)
end

#<=>(other) ⇒ Object

Provides a comparision operation for the nodes. Comparision is based on the natural character-set ordering for the node names.



143
144
145
146
# File 'lib/apotomo/tree_node.rb', line 143

def <=>(other)
  return +1 if other == nil
  self.name <=> other.name
end

#[](name) ⇒ Object

Returns the requested node from the set of immediate children.

If the key is numeric, then the in-sequence array of children is accessed (see Tree#children). If the key is not numeric, then it is assumed to be the name of the child node to be returned.



113
114
115
116
117
118
119
# File 'lib/apotomo/tree_node.rb', line 113

def [](name)
  if name.kind_of?(Integer)
    children[name]
  else
    childrenHash[name]
  end
end

#add(child) ⇒ Object

Adds the specified child node to the receiver node. The child node’s parent is set to be the receiver. The child is added as the last child in the current list of children for the receiver node.



39
40
41
42
43
44
45
46
47
48
# File 'lib/apotomo/tree_node.rb', line 39

def add(child)
  raise "Child already added" if @childrenHash.has_key?(child.name)

  @childrenHash[child.name]  = child
  @children << child
  child.parent = self
  
  child.run_widget_hook(:after_add, child, self)
  child
end

#childrenObject

Returns an array of all the immediate children. If a block is given, yields each child node to the block.



91
92
93
94
95
96
97
# File 'lib/apotomo/tree_node.rb', line 91

def children
  if block_given?
    @children.each { |child| yield child }
  else
    @children
  end
end

#each {|_self| ... } ⇒ Object

Returns every node (including the receiver node) from the tree to the specified block.

Yields:

  • (_self)

Yield Parameters:

  • _self (TreeNode)

    the object that the method was called on



101
102
103
104
# File 'lib/apotomo/tree_node.rb', line 101

def each(&block)
  yield self
  children { |child| child.each(&block) }
end

#find_by_path(selector) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/apotomo/tree_node.rb', line 150

def find_by_path(selector)
  next_node = self
  last      = nil # prevents self-finding loop.
  selector.to_s.split(/ /).each do |node_id| 
    last = next_node = next_node.find {|n|
      n.name.to_s == node_id.to_s and not n==last
    }
  end
  
  next_node
end

#initialize_tree_nodeObject



14
15
16
17
18
19
# File 'lib/apotomo/tree_node.rb', line 14

def initialize_tree_node(*)
  root!

  @childrenHash = {}
  @children = []
end

#pathObject

Returns the path from the widget to root, encoded as a string of slash-seperated names.



165
166
167
168
169
170
171
172
173
174
# File 'lib/apotomo/tree_node.rb', line 165

def path
  path      = [name]     
  ancestor  = parent
  while ancestor
    path << ancestor.name
    ancestor = ancestor.parent
  end
  
  path.reverse.join("/")
end

#printTree(tab = 0) ⇒ Object

Pretty prints the tree starting with the receiver node.



128
129
130
131
# File 'lib/apotomo/tree_node.rb', line 128

def printTree(tab = 0)
  puts((' ' * tab) + self.to_s)
  children {|child| child.printTree(tab + 4)}
end

#remove!(child) ⇒ Object

Removes the specified child node from the receiver node. The removed children nodes are orphaned but available if an alternate reference exists. Returns the child node.



54
55
56
57
58
59
# File 'lib/apotomo/tree_node.rb', line 54

def remove!(child)
  @childrenHash.delete(child.name)
  @children.delete(child)
  child.root! unless child == nil
  child
end

#remove_all!Object

Removes all children from the receiver node.



68
69
70
71
72
73
74
75
# File 'lib/apotomo/tree_node.rb', line 68

def remove_all!
  for child in @children
    child.root!
  end
  @childrenHash.clear
  @children.clear
  self
end

#remove_from_parent!Object

Removes this node from its parent. If this is the root node, then does nothing.



63
64
65
# File 'lib/apotomo/tree_node.rb', line 63

def remove_from_parent!
  @parent.remove!(self) unless root?
end

#rootObject

Returns the root for this node.



134
135
136
137
138
# File 'lib/apotomo/tree_node.rb', line 134

def root
  root = self
  root = root.parent while !root.root?
  root
end

#root?Boolean

Indicates whether this node is a root node. Note that orphaned children will also be reported as root nodes.

Returns:

  • (Boolean)


85
86
87
# File 'lib/apotomo/tree_node.rb', line 85

def root?
  @parent == nil
end

#sizeObject

Returns the total number of nodes in this tree, rooted at the receiver node.



123
124
125
# File 'lib/apotomo/tree_node.rb', line 123

def size
  children.inject(1) {|sum, node| sum + node.size}
end

#to_sObject

Print the string representation of this node.



22
23
24
25
# File 'lib/apotomo/tree_node.rb', line 22

def to_s
  "Node ID: #{widget_id} Parent: " + (root?  ? "ROOT" : "#{parent.name}") +
    " Children: #{children.length}" + " Total Nodes: #{size}"
end