Sha256: b3e99397f5e613684ba50ef9b47aa351220fe0b10dadd73df26221c364413031

Contents?: true

Size: 1.42 KB

Versions: 14

Compression:

Stored size: 1.42 KB

Contents

module TaxGenerator
  # node from the tree
  class TaxonomyNode < Tree::TreeNode
    #  prints the entire tree with name and content
    #
    # @param  [Integer]  level the level of the current node, Default 0
    # @param  [Integer]  max_depth the maximum depth the tree must be printed. Default nil
    # @param  [Lambda]  block the lambda that will be executed for printing node name and content
    #
    # @return [String]
    #
    # @api public
    def print_tree(level = 0, max_depth = nil, block = ->(node, prefix) { puts "#{prefix} #{node.respond_to?(:name) ? node.name : node}" })
      prefix = fetch_prefix_for_printing(level)
      block.call("#{name}---#{content}", prefix)
      return unless max_depth.nil? || level < max_depth # Exit if the max level is defined, and reached.

      children { |child| child.print_tree(level + 1, max_depth, block) if child } # Child might be 'nil'
    end

    #  builds up the prefix needed to display for current node
    #
    # @param  [Integer]  level the level of the current node
    #
    # @return [String]
    #
    # @api public
    def fetch_prefix_for_printing(level)
      prefix = ''
      if is_root?
        prefix << '*'
      else
        prefix << '|' unless parent.is_last_sibling?
        prefix << (' ' * (level - 1) * 4)
        prefix << (is_last_sibling? ? '+' : '|')
        prefix << '---'
        prefix << (has_children? ? '+' : '>')
      end
      prefix
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
tax_generator-0.8.0 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.7.1 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.7.0 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.6.6 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.6.5 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.6.4 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.6.3 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.6.2 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.6.1 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.6.0 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.5.2 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.5.1 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.5.0 lib/tax_generator/classes/taxonomy_node.rb
tax_generator-0.4.0 lib/tax_generator/classes/taxonomy_node.rb