Class: MultirootTree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/multiroot_tree.rb

Overview

Danny Pham 12/10/12

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ MultirootTree

Returns a new instance of MultirootTree.



16
17
18
19
20
# File 'lib/multiroot_tree.rb', line 16

def initialize(content)
  @content = content
  @children = []
  @parents = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



14
15
16
# File 'lib/multiroot_tree.rb', line 14

def children
  @children
end

#contentObject (readonly)

Returns the value of attribute content.



14
15
16
# File 'lib/multiroot_tree.rb', line 14

def content
  @content
end

#parentsObject (readonly)

Returns the value of attribute parents.



14
15
16
# File 'lib/multiroot_tree.rb', line 14

def parents
  @parents
end

Instance Method Details

#add_child(child) ⇒ Object

add a child to the children array, instance variable



23
24
25
26
27
28
# File 'lib/multiroot_tree.rb', line 23

def add_child(child)
  if child.instance_of? MultirootTree
    @children << child unless @children.include? child
    child.add_parent(self) unless child.parents.include?(self)
  end
end

#add_descendant_line(*descendants) ⇒ Object

adds a list of MultirootTrees with each successive MultirootTree being the child of the previous



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/multiroot_tree.rb', line 32

def add_descendant_line(*descendants)
  max = descendants.size
  num = 0
  first = self
  second = descendants[num]
  
  while (num < descendants.size)
    first.add_child(second)
    first = second
    second = descendants[num + 1]
    num += 1
  end
end

#add_parent(parent) ⇒ Object

adding parents is particularly necessary because any one node can have N parents, not just one



62
63
64
65
# File 'lib/multiroot_tree.rb', line 62

def add_parent(parent)
  @parents << parent unless @parents.include? parent
  parent.add_child(self) unless parent.children.include?(self)
end

#add_parents(*parents) ⇒ Object

adds immediate parents, not an ancestor line



68
69
70
71
72
73
# File 'lib/multiroot_tree.rb', line 68

def add_parents(*parents)
  parents.each do |parent| 
    self.add_parent(parent)
    parent.add_child(self) unless parent.children.include?(self)
  end
end

#disowns(child) ⇒ Object

severs tree connections going down descendant line



47
48
49
50
51
52
# File 'lib/multiroot_tree.rb', line 47

def disowns(child)
  if child.is_a? MultirootTree
    @children -= [child]
    child.emancipates_from(self) if child.parents.include?(self)
  end
end

#emancipates_from(parent) ⇒ Object

severs tree connections going up the ancestry



55
56
57
58
# File 'lib/multiroot_tree.rb', line 55

def emancipates_from(parent)
  @parents -= [parent]
  parent.disowns(self) if parent.children.include?(self) 
end

#floorObject

returns “youngest” children



87
88
89
90
91
92
93
94
95
# File 'lib/multiroot_tree.rb', line 87

def floor
  floor = []
  if @children.flatten.size == 0
    floor << MultirootTree.new(self.content)
  else
    @children.each {|child| floor << child.floor}
  end
  floor.flatten
end

#inspectObject

returns value of node



98
99
100
# File 'lib/multiroot_tree.rb', line 98

def inspect
  @content.inspect
end

#rootsObject

returns roots/greatest parents



76
77
78
79
80
81
82
83
84
# File 'lib/multiroot_tree.rb', line 76

def roots
  roots ||= []
  if @parents.flatten.size == 0
    roots << self
  else
    @parents.each {|parent| roots << parent.roots}
  end
  roots.flatten
end