Class: Hirb::Helpers::Tree::Node
- Inherits:
-
Hash
- Object
- Hash
- Hirb::Helpers::Tree::Node
show all
- Defined in:
- lib/hirb/helpers/tree.rb
Overview
Defined Under Namespace
Classes: MissingLevelError, MissingValueError
Instance Method Summary
collapse
Constructor Details
#initialize(hash) ⇒ Node
Returns a new instance of Node.
142
143
144
145
146
147
|
# File 'lib/hirb/helpers/tree.rb', line 142
def initialize(hash)
super
raise MissingLevelError unless hash.has_key?(:level)
raise MissingValueError unless hash.has_key?(:value)
replace(hash)
end
|
Instance Method Details
#get_parents_character(parent_chars) ⇒ Object
174
175
176
177
178
179
|
# File 'lib/hirb/helpers/tree.rb', line 174
def get_parents_character(parent_chars)
if self.parent
parent_chars << (self.parent[:last_node] ? ' ' : '|') unless self.parent.root?
self.parent.get_parents_character(parent_chars)
end
end
|
#next ⇒ Object
157
158
159
|
# File 'lib/hirb/helpers/tree.rb', line 157
def next
self[:tree].nodes[self[:index] + 1]
end
|
#parent ⇒ Object
153
154
155
|
# File 'lib/hirb/helpers/tree.rb', line 153
def parent
self[:tree].nodes.slice(0 .. self[:index]).reverse.detect {|e| e[:level] < self[:level]}
end
|
#previous ⇒ Object
161
162
163
|
# File 'lib/hirb/helpers/tree.rb', line 161
def previous
self[:tree].nodes[self[:index] - 1]
end
|
#render_parent_characters ⇒ Object
refers to characters which connect parent nodes
168
169
170
171
172
|
# File 'lib/hirb/helpers/tree.rb', line 168
def render_parent_characters
parent_chars = []
get_parents_character(parent_chars)
parent_chars.reverse.map {|level| level + ' ' * 3 }.join('')
end
|
#root? ⇒ Boolean
165
|
# File 'lib/hirb/helpers/tree.rb', line 165
def root?; self[:level] == 0; end
|
#value(indent = nil) ⇒ Object
149
150
151
|
# File 'lib/hirb/helpers/tree.rb', line 149
def value(indent=nil)
indent ? self[:value].gsub("\n", "\n#{indent * self[:level]}") : self[:value]
end
|