Sha256: 8a6316fd0d75361a9e8141b0f762e8875d8f7b3c8b3c2855382fa7f5a2bb2653

Contents?: true

Size: 788 Bytes

Versions: 13

Compression:

Stored size: 788 Bytes

Contents

# -*- coding: utf-8 -*-

module TreeRb
  #
  # Build hash with directory structure
  #
  class DirectoryToHashVisitor # < TreeVisitor::BasicTreeNodeVisitor

    attr_reader :root

    def initialize(pathname)
      @stack = []
      @node = {}
      @root = @node
    end

    def enter_node(pathname)
      subnode                        = {}
      @node[File.basename(pathname)] = subnode
      @stack.push(@node)
      @node = subnode
    end

    def exit_node(pathname)
      @node = @stack.pop
    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(pathname)
      @node[File.basename(pathname)] = File.stat(pathname).size
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
tree.rb-0.3.13 lib/tree_rb/visitors_file_system/directory_to_hash_visitor.rb
tree.rb-0.3.12 lib/tree_rb/visitors_file_system/directory_to_hash_visitor.rb
tree.rb-0.3.11 lib/tree_rb/visitors_file_system/directory_to_hash_visitor.rb
tree.rb-0.3.10 lib/tree_rb/visitors/directory_to_hash_visitor.rb
tree.rb-0.3.9 lib/tree_rb/visitors/directory_to_hash_visitor.rb
tree.rb-0.3.8 lib/tree_rb/visitors/directory_to_hash_visitor.rb
tree.rb-0.3.7 lib/tree_rb/visitors/directory_to_hash_visitor.rb
tree.rb-0.3.6 lib/tree_rb/visitors/directory_to_hash_visitor.rb
tree.rb-0.3.5 lib/tree_rb/visitors/directory_to_hash_visitor.rb
tree.rb-0.3.4 lib/tree_rb/visitors/directory_to_hash_visitor.rb
tree.rb-0.3.3 lib/tree_rb/visitors/directory_to_hash_visitor.rb
tree.rb-0.3.2 lib/tree_rb/visitors/directory_to_hash_visitor.rb
tree.rb-0.3.1 lib/tree_rb/visitors/directory_to_hash_visitor.rb