Sha256: 768088bcf95ccd6c1ed3569862cc2e9214ba1cf4eaa81322dd592fe5df737b53

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# -*- coding: utf-8 -*-
module TreeRb
  #
  # Builds a TreeNode from a filesystem directory
  # It similar to CloneTreeNodeVisitor
  #
  class BuildDirTreeVisitor # < BasicTreeNodeVisitor

    attr_reader :root

    #
    # Number of visited directory (aka nr_nodes - nr_leaf)
    #
    attr_reader :nr_directories


    #
    # Number of visited directory (nr_leaves)
    # @see AbsNode#nr_leaves
    #
    attr_reader :nr_files

    def initialize
      super
      @root = nil
      @stack = []
      @nr_directories = 0
      @nr_files = 0
    end

    def enter_node( pathname )
      if @stack.empty?
        tree_node = TreeNode.new( File.basename( pathname ) )
        @root = tree_node
      else
        tree_node = TreeNode.new( File.basename( pathname ), @stack.last )
      end
      @nr_directories += 1
      @stack.push( tree_node )
    end

    def cannot_enter_node(pathname, error)
    end

    def exit_node( pathname )
      @stack.pop
    end

    def visit_leaf( pathname )
      @nr_files  += 1
      # connect the leaf_node created to the last tree_node on the stack
      LeafNode.new( File.basename(pathname), @stack.last )
    end

  end
end # end module TreeVisitor

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tree.rb-0.3.1 lib/tree_rb/visitors/build_dir_tree_visitor.rb