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

Instance Method Details

#generate_structure(path = '.', exclude: [], suffix: nil, max_depth: nil) ⇒ Array<Hash>

Generate a nested hash representation of a directory tree.

Examples:

Basic usage

generate_structure(
  '/tmp',
  exclude: ['cache', 'logs'],
  max_depth: 2
)

Parameters:

  • path (String, Pathname) (defaults to: '.')

    The root directory to walk. Defaults to the current working directory (‘“.”`).

  • exclude (Array<String, Pathname>) (defaults to: [])

    Glob patterns (relative to path) that should be ignored during traversal.

  • suffix (String, nil) (defaults to: nil)

    Optional file extension filter (e.g., ‘rb’). If provided, only files with this suffix are included in the result. Pass ‘nil` or an empty string to include all files.

  • max_depth (Integer, nil) (defaults to: nil)

    Optional depth limit. If ‘nil`, the entire tree is returned. When an integer is supplied, all entries deeper than that depth are pruned.

Returns:

  • (Array<Hash>)

    An array of entry hashes. Each hash contains:

    • ‘:type` – “file” or “directory”

    • ‘:name` – Base name of the file/directory

    • ‘:path` – Absolute path

    • ‘:depth` – Depth relative to the root (root = 0)

    • ‘:height` – The maximum depth found in the entire tree

    • ‘:children` – Array of child entry hashes (only for directories)

Raises:

  • (StandardError)

    Any exception raised during traversal is rescued and returned as a hash with ‘:error` and `:message` keys.



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.message }
end