Sha256: 756514e16684c03e220c339507579e92d4220af588243471f5818f8b3e8d4976

Contents?: true

Size: 1.21 KB

Versions: 45

Compression:

Stored size: 1.21 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 or proc 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)
      @value_method = value_method.is_a?(Proc) ? value_method : lambda {|n| n.send(value_method) }
      children_method = options[:children_method] || :children
      @children_method = children_method.is_a?(Proc) ? children_method : lambda {|n| n.send(children_method)}
      @nodes = []
      build_node(root_node, 0)
      super(@nodes, options)
    end

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

Version data entries

45 entries across 44 versions & 5 rubygems

Version Path
hirber-0.8.7 lib/hirb/helpers/parent_child_tree.rb
hirber-0.8.6 lib/hirb/helpers/parent_child_tree.rb
hirber-0.8.5 lib/hirb/helpers/parent_child_tree.rb
hirber-0.8.4 lib/hirb/helpers/parent_child_tree.rb
hirber-0.8.2 lib/hirb/helpers/parent_child_tree.rb
hirber-0.8.1 lib/hirb/helpers/parent_child_tree.rb
hirber-0.8.0 lib/hirb/helpers/parent_child_tree.rb
hirb-0.7.3 lib/hirb/helpers/parent_child_tree.rb
climine-0.0.7 vendor/bundle/ruby/2.0.0/gems/hirb-0.7.1/lib/hirb/helpers/parent_child_tree.rb
climine-0.0.7 vendor/bundle/ruby/2.1.0/gems/hirb-0.7.1/lib/hirb/helpers/parent_child_tree.rb
hirb-0.7.2 lib/hirb/helpers/parent_child_tree.rb
climine-0.0.6 vendor/bundle/ruby/2.0.0/gems/hirb-0.7.1/lib/hirb/helpers/parent_child_tree.rb
climine-0.0.5 vendor/bundle/ruby/2.0.0/gems/hirb-0.7.1/lib/hirb/helpers/parent_child_tree.rb
climine-0.0.4 vendor/bundle/ruby/2.0.0/gems/hirb-0.7.1/lib/hirb/helpers/parent_child_tree.rb
climine-0.0.3 vendor/bundle/ruby/2.0.0/gems/hirb-0.7.1/lib/hirb/helpers/parent_child_tree.rb
climine-0.0.2 vendor/bundle/ruby/2.0.0/gems/hirb-0.7.1/lib/hirb/helpers/parent_child_tree.rb
climine-0.0.1 vendor/bundle/ruby/2.0.0/gems/hirb-0.7.1/lib/hirb/helpers/parent_child_tree.rb
hirb-0.7.1 lib/hirb/helpers/parent_child_tree.rb
hirb-0.7.0 lib/hirb/helpers/parent_child_tree.rb
hirb-0.6.2 lib/hirb/helpers/parent_child_tree.rb