Class: Hirb::Helpers::ParentChildTree

Inherits:
Tree
  • Object
show all
Defined in:
lib/hirb/helpers/parent_child_tree.rb

Instance Attribute Summary

Attributes inherited from Tree

#nodes

Class Method Summary collapse

Methods inherited from Tree

#initialize, #mark_last_nodes_per_level, #render, #render_basic, #render_description, #render_directory, #render_number, #render_tree, #validate_nodes

Constructor Details

This class inherits a constructor from Hirb::Helpers::Tree

Class Method Details

.build_node(node, level) ⇒ Object

:nodoc:



17
18
19
20
21
# File 'lib/hirb/helpers/parent_child_tree.rb', line 17

def build_node(node, level) #:nodoc:
  @nodes << {:value=>node.send(@value_method), :level=>level}
  children = @children_method.is_a?(Proc) ? @children_method.call(node) : node.send(@children_method)
  children.each {|e| build_node(e, level + 1)}
end

.render(root_node, options = {}) ⇒ Object

Starting with the given node, this builds a tree by recursively calling a children method. Takes same options as Hirb::Helper::Table.render with some additional ones below.

Options:

:value_method

Method to call to display as a node’s value. If not given, uses :name if node responds to :name or defaults to :object_id.

:children_method

Method or proc to call to obtain a node’s children. Default is :children.



9
10
11
12
13
14
15
# File 'lib/hirb/helpers/parent_child_tree.rb', line 9

def render(root_node, options={})
  @value_method = options[:value_method] || (root_node.respond_to?(:name) ? :name : :object_id)
  @children_method = options[:children_method] || :children
  @nodes = []
  build_node(root_node, 0)
  super(@nodes, options)
end