Sha256: 8fc7bbcaf88b7f17b68069be7af1434beb88c8b84eac4843731c76a11b8c48e6

Contents?: true

Size: 856 Bytes

Versions: 2

Compression:

Stored size: 856 Bytes

Contents

# -*- coding: utf-8 -*-
module TreeRb
  #
  # Prints TreeNode names indenting according to depth
  #
  class PrintTreeNodeVisitor # < BasicTreeNodeVisitor

    def initialize
      @depth = 0
    end

    def enter_node( tree_node )
      str = ''
      (0...@depth).step {
        str << ' |-'
      }

      if @depth == 0
        puts str + tree_node.name.to_s
      else
        puts str + tree_node.name.to_s
      end
      @depth += 1
    end

    def exit_node( tree_node )
      @depth -= 1
    end

    #
    # called when the tree node is not accessible or an exception is raise when the node is accessed
    #
    def cannot_enter_node( tree_node, error)
    end

    def visit_leaf( leaf_node )
      str = ""
      (0...@depth-1).step {
        str << ' |-'
      }
      str << ' |  '
      puts str + leaf_node.name.to_s
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tree.rb-0.3.13 lib/tree_rb/visitors/print_tree_node_visitor.rb
tree.rb-0.3.12 lib/tree_rb/visitors/print_tree_node_visitor.rb