Class: HTOTConv::Outline::Tree
- Inherits:
-
Object
- Object
- HTOTConv::Outline::Tree
- Includes:
- Enumerable
- Defined in:
- lib/htot_conv/outline.rb
Instance Attribute Summary collapse
-
#item ⇒ Object
Returns the value of attribute item.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #add(item) ⇒ Object (also: #<<)
- #ancestors ⇒ Object
- #descendants ⇒ Object
-
#each ⇒ Object
:yield: child.
-
#initialize(item = nil, parent = nil) ⇒ Tree
constructor
A new instance of Tree.
- #leaf? ⇒ Boolean
- #next ⇒ Object
- #prev ⇒ Object
- #root ⇒ Object
- #root? ⇒ Boolean
Constructor Details
#initialize(item = nil, parent = nil) ⇒ Tree
Returns a new instance of Tree.
98 99 100 101 102 |
# File 'lib/htot_conv/outline.rb', line 98 def initialize(item=nil, parent=nil) @item = item @parent = parent @children = [] end |
Instance Attribute Details
#item ⇒ Object
Returns the value of attribute item.
103 104 105 |
# File 'lib/htot_conv/outline.rb', line 103 def item @item end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
104 105 106 |
# File 'lib/htot_conv/outline.rb', line 104 def parent @parent end |
Instance Method Details
#add(item) ⇒ Object Also known as: <<
114 115 116 117 118 |
# File 'lib/htot_conv/outline.rb', line 114 def add(item) child = Tree.new(item, self) @children << child self end |
#ancestors ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/htot_conv/outline.rb', line 154 def ancestors Enumerator.new do |y| node = self.parent until (node.nil? || node.root?) y << node node = node.parent end end end |
#descendants ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/htot_conv/outline.rb', line 164 def descendants Enumerator.new do |y| @children.each do |child| y << child child.descendants.each do |descendant| y << descendant end end end end |
#each ⇒ Object
:yield: child
121 122 123 124 125 126 |
# File 'lib/htot_conv/outline.rb', line 121 def each # :yield: child @children.each do |v| yield v if block_given? end @children.dup end |
#leaf? ⇒ Boolean
110 111 112 |
# File 'lib/htot_conv/outline.rb', line 110 def leaf? @children.empty? end |
#next ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/htot_conv/outline.rb', line 134 def next if root? nil else brothers = parent.to_a index = brothers.index(self) (index + 1 < brothers.length)? brothers[index + 1] : nil end end |
#prev ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/htot_conv/outline.rb', line 144 def prev if root? nil else brothers = parent.to_a index = brothers.index(self) (index - 1 >= 0)? brothers[index - 1] : nil end end |
#root ⇒ Object
128 129 130 131 132 |
# File 'lib/htot_conv/outline.rb', line 128 def root node = self node = node.parent until node.root? node end |
#root? ⇒ Boolean
106 107 108 |
# File 'lib/htot_conv/outline.rb', line 106 def root? @parent.nil? end |