Class: TreeRb::DirectoryToHash2Visitor

Inherits:
BasicTreeNodeVisitor show all
Defined in:
lib/tree_rb/output_plugins/html/directory_to_hash2_visitor.rb

Overview

Build hash with directory structure

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BasicTreeNodeVisitor

#cannot_enter_node, #cannot_visit_leaf

Constructor Details

#initialize(pathname) ⇒ DirectoryToHash2Visitor

Returns a new instance of DirectoryToHash2Visitor.



81
82
83
# File 'lib/tree_rb/output_plugins/html/directory_to_hash2_visitor.rb', line 81

def initialize(pathname)
  @stack = []
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



79
80
81
# File 'lib/tree_rb/output_plugins/html/directory_to_hash2_visitor.rb', line 79

def root
  @root
end

Instance Method Details

#enter_node(pathname) ⇒ Object



85
86
87
88
89
90
# File 'lib/tree_rb/output_plugins/html/directory_to_hash2_visitor.rb', line 85

def enter_node(pathname)
  @root = @node if @root.nil?
  @node = { name: File.basename(pathname), children: [] }
  @stack.last[:children] << @node unless @stack.empty?
  @stack.push(@node)
end

#exit_node(pathname) ⇒ Object



92
93
94
# File 'lib/tree_rb/output_plugins/html/directory_to_hash2_visitor.rb', line 92

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

#visit_leaf(pathname) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/tree_rb/output_plugins/html/directory_to_hash2_visitor.rb', line 96

def visit_leaf(pathname)
  begin
    @node[:children] << { name: File.basename(pathname), size: File.stat(pathname).size }
  rescue Errno::ENOENT => e
    $stderr.puts e.to_s
  end
end