Class: HTOTConv::Outline::Tree

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#itemObject

Returns the value of attribute item.



103
104
105
# File 'lib/htot_conv/outline.rb', line 103

def item
  @item
end

#parentObject (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

#ancestorsObject



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

#descendantsObject



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

#eachObject

: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

Returns:

  • (Boolean)


110
111
112
# File 'lib/htot_conv/outline.rb', line 110

def leaf?
  @children.empty?
end

#nextObject



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

#prevObject



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

#rootObject



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

Returns:

  • (Boolean)


106
107
108
# File 'lib/htot_conv/outline.rb', line 106

def root?
  @parent.nil?
end