Class: TreeRb::DirectoryToHashVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_rb/visitors_file_system/directory_to_hash_visitor.rb

Overview

Build hash with directory structure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pathname) ⇒ DirectoryToHashVisitor

Returns a new instance of DirectoryToHashVisitor.



11
12
13
14
15
# File 'lib/tree_rb/visitors_file_system/directory_to_hash_visitor.rb', line 11

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

Instance Attribute Details

#rootObject (readonly)

< TreeVisitor::BasicTreeNodeVisitor



9
10
11
# File 'lib/tree_rb/visitors_file_system/directory_to_hash_visitor.rb', line 9

def root
  @root
end

Instance Method Details

#cannot_enter_node(tree_node, error) ⇒ Object

called when the tree node is not accessible or an exception is raise when the node is accessed



31
32
# File 'lib/tree_rb/visitors_file_system/directory_to_hash_visitor.rb', line 31

def cannot_enter_node( tree_node, error)
end

#enter_node(pathname) ⇒ Object



17
18
19
20
21
22
# File 'lib/tree_rb/visitors_file_system/directory_to_hash_visitor.rb', line 17

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

#exit_node(pathname) ⇒ Object



24
25
26
# File 'lib/tree_rb/visitors_file_system/directory_to_hash_visitor.rb', line 24

def exit_node(pathname)
  @node = @stack.pop
end

#visit_leaf(pathname) ⇒ Object



34
35
36
# File 'lib/tree_rb/visitors_file_system/directory_to_hash_visitor.rb', line 34

def visit_leaf(pathname)
  @node[File.basename(pathname)] = File.stat(pathname).size
end