Module: Apotomo::TreeNode

Includes:
WidgetShortcuts::DSL, Enumerable
Included in:
Widget
Defined in:
lib/apotomo/widget/tree_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WidgetShortcuts::DSL

#<<

Instance Attribute Details

#childrenHashObject (readonly)

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



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

def childrenHash
  @childrenHash
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#<=>(other) ⇒ Object

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



115
116
117
118
# File 'lib/apotomo/widget/tree_node.rb', line 115

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.



84
85
86
87
88
89
90
# 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) ⇒ Object

TODO: rename #add, make private



27
28
29
30
31
32
33
34
35
# 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

#childrenObject

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



62
63
64
65
66
67
68
# 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| ... } ⇒ Object

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

Yields:

  • (_self)

Yield Parameters:



72
73
74
75
# File 'lib/apotomo/widget/tree_node.rb', line 72

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

#find_by_path(selector) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# 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

#pathObject

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



137
138
139
140
141
142
143
144
145
146
# 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) ⇒ Object

Pretty prints the tree starting with the receiver node.



99
100
101
# File 'lib/apotomo/widget/tree_node.rb', line 99

def printTree(tab = 0)
  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.



41
42
43
44
45
46
47
# 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

#rootObject

Returns the root for this node.



104
105
106
107
108
# File 'lib/apotomo/widget/tree_node.rb', line 104

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)


56
57
58
# File 'lib/apotomo/widget/tree_node.rb', line 56

def root?
  @parent == nil
end

#setup_tree_node(parent) ⇒ Object

DISCUSS: make private?



11
12
13
14
15
16
17
18
# 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

#sizeObject

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



94
95
96
# File 'lib/apotomo/widget/tree_node.rb', line 94

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

#to_sObject

Print the string representation of this node.



21
22
23
24
25
# 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