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



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

#nextObject



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

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

#parentObject



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

#previousObject



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

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

#render_parent_charactersObject

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

Returns:

  • (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