Class: Hirb::Helpers::Tree::Node

Inherits:
Hash
  • Object
show all
Defined in:
lib/hirb/helpers/tree.rb

Overview

:startdoc:

Defined Under Namespace

Classes: MissingLevelError, MissingValueError

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Node

Returns a new instance of Node.

Raises:



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



170
171
172
173
174
175
# File 'lib/hirb/helpers/tree.rb', line 170

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

#nextObject



153
154
155
# File 'lib/hirb/helpers/tree.rb', line 153

def next
  self[:tree].nodes[self[:index] + 1]
end

#parentObject



149
150
151
# File 'lib/hirb/helpers/tree.rb', line 149

def parent
  self[:tree].nodes.slice(0 .. self[:index]).reverse.detect {|e| e[:level] < self[:level]}
end

#previousObject



157
158
159
# File 'lib/hirb/helpers/tree.rb', line 157

def previous
  self[:tree].nodes[self[:index] - 1]
end

#render_parent_charactersObject

refers to characters which connect parent nodes



164
165
166
167
168
# File 'lib/hirb/helpers/tree.rb', line 164

def render_parent_characters
  parent_chars = []
  get_parents_character(parent_chars)
  parent_chars.reverse.map {|level| level + ' ' * 3 }.to_s
end

#root?Boolean

Returns:

  • (Boolean)


161
# File 'lib/hirb/helpers/tree.rb', line 161

def root?; self[:level] == 0; end