Class: Ruborg::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/ruborg/catalog.rb

Overview

Read-only view over the ArchiveCache for searching and reporting. Never writes back to the cache.

Instance Method Summary collapse

Constructor Details

#initialize(repo_path) ⇒ Catalog

Returns a new instance of Catalog.



7
8
9
# File 'lib/ruborg/catalog.rb', line 7

def initialize(repo_path)
  @cache = ArchiveCache.new(repo_path).fetch
end

Instance Method Details

#listObject

Returns all cached entries sorted by file path.



12
13
14
# File 'lib/ruborg/catalog.rb', line 12

def list
  @cache.entries.sort_by { |e| e[:path].to_s }
end

#search(pattern) ⇒ Object

Returns entries whose :path matches pattern (a Regexp or regex string). Raises CatalogError on invalid regex.



18
19
20
21
22
23
# File 'lib/ruborg/catalog.rb', line 18

def search(pattern)
  regex = pattern.is_a?(Regexp) ? pattern : Regexp.new(pattern)
  list.select { |e| regex.match?(e[:path].to_s) }
rescue RegexpError => e
  raise CatalogError, "Invalid regex pattern: #{e.message}"
end

#statsObject

Returns a summary hash with aggregate statistics.



26
27
28
29
30
31
32
33
34
# File 'lib/ruborg/catalog.rb', line 26

def stats
  all = list
  {
    total_archives: all.size,
    unique_paths: all.map { |e| e[:path] }.uniq.size,
    total_size: all.sum { |e| e[:size].to_i },
    source_dirs: all.map { |e| e[:source_dir] }.uniq.reject(&:empty?).size
  }
end