Module: DirTree

Defined in:
lib/dir_tree.rb,
lib/dir_tree/version.rb

Overview

Convert directory tree structure into a hash

Parameters:

path

Root directory path for a hash

name

Do not specify explicitly as it is used for recursion

Returns:

A hash representing the directory tree structure

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.hash_tree(path, name = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dir_tree.rb', line 16

def self.hash_tree(path, name = nil)
  key = name || path
  data = {}
  data[key] = children = {}
  Dir.foreach(path) do |entry|
    next if entry == '..' || entry == '.'
    full_path = File.join(path, entry)
    if File.directory?(full_path)
      children.update(hash_tree(full_path, entry))
    else
      children[entry] = nil
    end
  end
  data
end