module Apotomo::TreeNode

Attributes

childrenHash[R]

DISCUSS: do we need it? we have []! DISCUSS: children receives a block, but childrenHash doesn’t

parent[RW]

Public Instance Methods

<=>(other) click to toggle source

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
[](name) click to toggle source

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
add_widget(child) click to toggle source
# 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
children() { |child| ... } click to toggle source

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
each() { |self| ... } click to toggle source

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
find_by_path(selector) click to toggle source
# 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
path() click to toggle source

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
printTree(tab = 0) click to toggle source

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
remove!(child) click to toggle source

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
root() click to toggle source

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
root?() click to toggle source

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
setup_tree_node(parent) click to toggle source
# 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
size() click to toggle source

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
to_s() click to toggle source

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

root!() click to toggle source

Private method which sets this node as a root node.

# File lib/apotomo/widget/tree_node.rb, line 50
def root!
  @parent = nil
end