tree.rb

lib/hirb/helpers/tree.rb
Last Update: Wed Apr 14 16:09:16 -0400 2010

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.