Sha256: 4336c9746c5e7c9e0900143e81efb16cfd7bd611cf1bc30c27be8569c2bbab4a

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

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 or proc 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}
      children = @children_method.is_a?(Proc) ? @children_method.call(node) : node.send(@children_method)
      children.each {|e| build_node(e, level + 1)}
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
cldwalker-hirb-0.2.6 lib/hirb/helpers/parent_child_tree.rb
hirb-0.2.7 lib/hirb/helpers/parent_child_tree.rb
hirb-0.2.6 lib/hirb/helpers/parent_child_tree.rb