Class Hirb::Helpers::Tree

  1. 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:
  • directory:
    |-- 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.


public class

  1. render

Classes and Modules

Class Hirb::Helpers::Tree::ParentlessNodeError

Public class methods

render (nodes, options={})

Main method which renders a tree.


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