Class Hirb::Helpers::Tree
In: lib/hirb/helpers/tree.rb
Parent: Object

Base tree class which given an array of nodes produces different types of trees. The types of trees currently are:

  • basic:
     0
       1
         2
         3
       4
    
  • directory:
     0
     |-- 1
     |   |-- 2
     |   `-- 3
     `-- 4
    
  • number:
     1. 0
       1. 1
         1. 2
         2. 3
       2. 4
    

Tree nodes can be given as an array of arrays or an array of hashes. To render the above basic tree with an array of hashes:

  Hirb::Helpers::Tree.render([{:value=>0, :level=>0}, {:value=>1, :level=>1}, {:value=>2, :level=>2},
    {:value=>3, :level=>2}, {:value=>4, :level=>1}])

Note from the hash keys that :level refers to the depth of the tree while :value refers to the text displayed for a node.

To render the above basic tree with an array of arrays:

  Hirb::Helpers::Tree.render([[0,0], [1,1], [2,2], [2,3], [1,4]])

Note that the each array pair consists of the level and the value for the node.

Methods

render  

Classes and Modules

Class Hirb::Helpers::Tree::ParentlessNodeError

Public Class methods

Main method which renders a tree.

Options:

:type
Type of tree. Either :basic, :directory or :number. Default is :basic.
:validate
Boolean to validate tree. Checks to see if all nodes have parents. Raises ParentlessNodeError if an invalid node is found. Default is false.
:indent
Number of spaces to indent between levels for basic + number trees. Default is 4.
:limit
Limits the level or depth of a tree that is displayed. Root node is level 0.
:description
Displays brief description about tree ie how many nodes it has.
:multi_line_nodes
Handles multi-lined nodes by indenting their newlines. Default is false.
 Examples:
    Hirb::Helpers::Tree.render([[0, 'root'], [1, 'child']], :type=>:directory)

[Validate]