Module: OllamaChat::Utils::AnalyzeDirectory
- Included in:
- Parsing
- Defined in:
- lib/ollama_chat/utils/analyze_directory.rb
Overview
The ‘OllamaChat::Utils::AnalyzeDirectory` module provides a small, dependency‑free helper for walking a directory tree and producing a nested hash representation of the file system.
It supports:
-
Recursive traversal of directories
-
Skipping hidden files/directories and symbolic links
-
Excluding arbitrary paths via glob patterns
-
Limiting the depth of the returned tree
Example
require_relative 'analyze_directory'
include OllamaChat::Utils::AnalyzeDirectory
structure = generate_structure(
'/path/to/dir',
exclude: ['tmp', 'vendor'],
max_depth: 3
)
puts structure.inspect
Instance Method Summary collapse
-
#generate_structure(path = '.', exclude: [], suffix: nil, max_depth: nil) ⇒ Array<Hash>
Generate a nested hash representation of a directory tree.
Instance Method Details
#generate_structure(path = '.', exclude: [], suffix: nil, max_depth: nil) ⇒ Array<Hash>
Generate a nested hash representation of a directory tree.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ollama_chat/utils/analyze_directory.rb', line 63 def generate_structure(path = '.', exclude: [], suffix: nil, max_depth: nil) entries = recurse_generate_structure(path, exclude:, suffix:) height = 0 structure_each_entry(entries) do |e| height = e[:depth] if e[:depth] > height end structure_each_entry(entries) { |e| e[:height] = height } if max_depth && max_depth < height structure_each_entry(entries) do |e| e[:children]&.reject! { |c| c[:depth] > max_depth } end end entries rescue => e { error: e.class, message: e. } end |