class DirTree

Returns a root directory as a tree of directories

Attributes

tree[R]

Public Class Methods

new(path) click to toggle source
# File lib/cpp_dependency_graph/dir_tree.rb, line 13
def initialize(path)
  @tree = File.directory?(path) ? parse_dirs(path) : {}
end

Private Instance Methods

parse_dirs(path, name = nil) click to toggle source
# File lib/cpp_dependency_graph/dir_tree.rb, line 19
def parse_dirs(path, name = nil)
  data = Hash.new { |h, k| h[k] = [] }
  data[:name] = (name || path)
  # TODO: Use Dir.map.compact|filter instead here
  Dir.foreach(path) do |entry|
    next if ['..', '.'].include?(entry)

    full_path = File.join(path, entry)
    next unless File.directory?(full_path)

    next unless source_files_present?(full_path)

    data[:children] << parse_dirs(full_path, entry)
  end
  data
end
source_files_present?(full_path) click to toggle source
# File lib/cpp_dependency_graph/dir_tree.rb, line 36
def source_files_present?(full_path)
  files = Dir.glob(File.join(full_path, File.join('**', '*' + source_file_extensions)))
  files.size.positive?
end
to_s() click to toggle source
# File lib/cpp_dependency_graph/dir_tree.rb, line 41
def to_s
  @tree.to_json
end