module Apotomo::TreeNode
Attributes
DISCUSS: do we need it? we have []! DISCUSS: children
receives a block, but childrenHash
doesn’t
Public Instance Methods
Provides a comparision operation for the nodes. Comparision is based on the natural character-set ordering for the node names. DUISCUSS: useful? DUISCUSS: <, >, etc., operators doesn’t work because of Comparable isn’t included
# File lib/apotomo/widget/tree_node.rb, line 115 def <=>(other) return +1 if other == nil self.name <=> other.name end
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.
# File lib/apotomo/widget/tree_node.rb, line 84 def [](name) if name.kind_of?(Integer) children[name] else childrenHash[name] end end
# File lib/apotomo/widget/tree_node.rb, line 27 def add_widget(child) # TODO: rename #add, make private raise "Child already added" if @childrenHash.has_key?(child.name) @childrenHash[child.widget_id] = child @children << child child.parent = self child end
Returns an array of all the immediate children. If a block is given, yields each child node to the block.
# File lib/apotomo/widget/tree_node.rb, line 62 def children if block_given? @children.each { |child| yield child } else @children end end
Returns every node (including the receiver node) from the tree to the specified block.
# File lib/apotomo/widget/tree_node.rb, line 72 def each(&block) yield self children { |child| child.each(&block) } end
# File lib/apotomo/widget/tree_node.rb, line 122 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
Returns the path from the widget to root, encoded as a string of slash-seperated names.
# File lib/apotomo/widget/tree_node.rb, line 137 def path path = [name] ancestor = parent while ancestor path << ancestor.name ancestor = ancestor.parent end path.reverse.join("/") end
Pretty prints the tree starting with the receiver node.
# File lib/apotomo/widget/tree_node.rb, line 99 def printTree(tab = 0) children {|child| child.printTree(tab + 4)} end
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.
# File lib/apotomo/widget/tree_node.rb, line 41 def remove!(child) @childrenHash.delete(child.name) @children.delete(child) # DISCUSS: why `unless child == nil`? if child is nil, an exception has been raised two locs above! child.root! unless child == nil child end
Returns the root for this node.
# File lib/apotomo/widget/tree_node.rb, line 104 def root root = self root = root.parent while !root.root? root end
Indicates whether this node is a root node. Note that orphaned children will also be reported as root nodes.
# File lib/apotomo/widget/tree_node.rb, line 56 def root? @parent == nil end
# File lib/apotomo/widget/tree_node.rb, line 11 def setup_tree_node(parent) # DISCUSS: make private? @parent = nil @childrenHash = {} @children = [] # TODO: order of widgets in this variable isn't tested anywhere!!! # DISCUSS: and what if not a Widget? parent.add_widget(self) if parent.kind_of? Widget # TODO: as long as cells needs parent_controller. end
Returns the total number of nodes in this tree, rooted at the receiver node.
# File lib/apotomo/widget/tree_node.rb, line 94 def size children.inject(1) {|sum, node| sum + node.size} end
Print the string representation of this node.
# File lib/apotomo/widget/tree_node.rb, line 21 def to_s # DISCUSS: why self.widget_id but parent.name ? "Node ID: #{widget_id} Parent: " + (root? ? "ROOT" : "#{parent.name}") + " Children: #{children.length}" + " Total Nodes: #{size}" end
Protected Instance Methods
Private method which sets this node as a root node.
# File lib/apotomo/widget/tree_node.rb, line 50 def root! @parent = nil end