Sha256: 04828e6506e5378c296cc3d02d21c3afb4237e6ad8da2acdac26c66aa3f3e70a

Contents?: true

Size: 1020 Bytes

Versions: 14

Compression:

Stored size: 1020 Bytes

Contents

class Hirb::Helpers::ParentChildTree < Hirb::Helpers::Tree
  class <<self
    # 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 to call to obtain a node's children. Default is :children.
    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

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

Version data entries

14 entries across 14 versions & 3 rubygems

Version Path
Chrononaut-hirb-0.2.1 lib/hirb/helpers/parent_child_tree.rb
Chrononaut-hirb-0.2.3 lib/hirb/helpers/parent_child_tree.rb
cldwalker-hirb-0.1.2 lib/hirb/helpers/parent_child_tree.rb
cldwalker-hirb-0.2.0 lib/hirb/helpers/parent_child_tree.rb
cldwalker-hirb-0.2.1 lib/hirb/helpers/parent_child_tree.rb
cldwalker-hirb-0.2.2 lib/hirb/helpers/parent_child_tree.rb
cldwalker-hirb-0.2.3 lib/hirb/helpers/parent_child_tree.rb
cldwalker-hirb-0.2.4 lib/hirb/helpers/parent_child_tree.rb
cldwalker-hirb-0.2.5 lib/hirb/helpers/parent_child_tree.rb
hirb-0.2.2 lib/hirb/helpers/parent_child_tree.rb
hirb-0.2.3 lib/hirb/helpers/parent_child_tree.rb
hirb-0.2.4 lib/hirb/helpers/parent_child_tree.rb
hirb-0.2.5 lib/hirb/helpers/parent_child_tree.rb
hirb-0.1.2 lib/hirb/helpers/parent_child_tree.rb