Class: TaxGenerator::TaxonomyNode

Inherits:
Tree::TreeNode
  • Object
show all
Defined in:
lib/tax_generator/classes/taxonomy_node.rb

Overview

node from the tree

Instance Method Summary collapse

Instance Method Details

#fetch_prefix_for_printing(level) ⇒ String

builds up the prefix needed to display for current node

Parameters:

  • level (Integer)

    the level of the current node

Returns:

  • (String)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tax_generator/classes/taxonomy_node.rb', line 28

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

prints the entire tree with name and content

Parameters:

  • level (Integer) (defaults to: 0)

    the level of the current node, Default 0

  • max_depth (Integer) (defaults to: nil)

    the maximum depth the tree must be printed. Default nil

  • block (Lambda) (defaults to: ->(node, prefix) { puts "#{prefix} #{node.respond_to?(:name) ? node.name : node}" })

    the lambda that will be executed for printing node name and content

Returns:

  • (String)


13
14
15
16
17
18
19
# File 'lib/tax_generator/classes/taxonomy_node.rb', line 13

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