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.
-
#recurse_generate_structure(path = '.', exclude: [], suffix: nil, depth: 0) ⇒ Array<Hash>
private
private
Recursively walk path and build the tree.
-
#structure_each_entry(entries) {|Hash| ... } ⇒ Array<Hash>
private
private
Iterate over every entry in entries (depth‑first).
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 |
#recurse_generate_structure(path = '.', exclude: [], suffix: nil, depth: 0) ⇒ Array<Hash> (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Recursively walk path and build the tree.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ollama_chat/utils/analyze_directory.rb', line 97 def recurse_generate_structure(path = '.', exclude: [], suffix: nil, depth: 0) exclude = Array(exclude).map { |p| Pathname.new(p). } extname = suffix&.sub(/\A(?<!.)/, ?.) path = Pathname.new(path). entries = [] path.children.sort.each do |child| # Skip hidden files/directories next if child.basename.to_s.start_with?('.') # Skip symlinks next if child.symlink? # Skip user‑excluded paths next if exclude.any? { |e| child.fnmatch?(e.to_s, File::FNM_PATHNAME) } if child.directory? entries << { type: 'directory', name: child.basename.to_s, path: child..to_s, children: recurse_generate_structure(child, exclude:, suffix:, depth: depth + 1), depth: } elsif child.file? # Skip unless suffix matches next if extname.present? && child.extname != extname entries << { type: 'file', name: child.basename.to_s, path: child..to_s, depth: } end end entries end |
#structure_each_entry(entries) {|Hash| ... } ⇒ Array<Hash> (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Iterate over every entry in entries (depth‑first).
142 143 144 145 146 147 148 149 |
# File 'lib/ollama_chat/utils/analyze_directory.rb', line 142 def structure_each_entry(entries, &block) queue = entries.dup while entry = queue.shift block.(entry) queue.concat(entry[:children]) if entry[:children] end entries end |