Module: OllamaChat::Utils::AnalyzeDirectory
- Defined in:
- lib/ollama_chat/utils/analyze_directory.rb
Overview
A module that provides functionality for analyzing directory structures and generating file listings.
The AnalyzeDirectory module offers methods to traverse directory hierarchies and create structured representations of file systems. It supports recursive directory traversal, filtering of hidden files and symbolic links, and generation of detailed file and directory information including paths, names, and metadata.
Class Method Summary collapse
-
.generate_structure(path = ?., exclude: []) ⇒ Array<Hash>, ...
Generates a directory structure representation with files and subdirectories.
Class Method Details
.generate_structure(path = ?., exclude: []) ⇒ Array<Hash>, ...
Hidden files and directories (starting with ‘.’) are skipped
Symbolic links are skipped
The method uses recursive calls to traverse subdirectories
If an error occurs during traversal, it returns a hash with error details
Generates a directory structure representation with files and subdirectories.
defaults to current directory
directories, or a hash with error information if an exception occurs
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ollama_chat/utils/analyze_directory.rb', line 33 def generate_structure(path = ?., exclude: []) exclude = Array(exclude).map { Pathname.new(_1). } 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 if excluded next if exclude.any? { child.fnmatch?(_1.to_s, File::FNM_PATHNAME) } if child.directory? entries << { type: 'directory', name: child.basename.to_s, path: child..to_s, children: generate_structure(child) } elsif child.file? entries << { type: 'file', path: child..to_s, name: child.basename.to_s } end end entries rescue => e { error: e.class, message: e. } end |