Class: Cabriolet::FileManager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cabriolet/file_manager.rb

Overview

Manages collection of files for archive creation

Single responsibility: File list management and enumeration. Provides unified interface for adding files from disk or memory, and supports standard Ruby enumeration patterns.

Examples:

Basic usage

manager = FileManager.new
manager.add_file("/path/to/file.txt", "docs/file.txt")
manager.add_data("Hello", "greeting.txt")
manager.each { |entry| puts entry.archive_path }

Instance Method Summary collapse

Constructor Details

#initializeFileManager

Initialize empty file manager



21
22
23
# File 'lib/cabriolet/file_manager.rb', line 21

def initialize
  @entries = []
end

Instance Method Details

#[](index) ⇒ FileEntry?

Get entry by index

Parameters:

  • index (Integer)

    Entry index

Returns:

  • (FileEntry, nil)

    Entry or nil if out of bounds



88
89
90
# File 'lib/cabriolet/file_manager.rb', line 88

def [](index)
  @entries[index]
end

#add_data(data, archive_path, **options) ⇒ FileEntry

Add file from memory

Parameters:

  • data (String)

    File data

  • archive_path (String)

    Path in archive

  • options (Hash)

    Format-specific options

Returns:



51
52
53
54
55
56
57
58
59
60
# File 'lib/cabriolet/file_manager.rb', line 51

def add_data(data, archive_path, **options)
  entry = FileEntry.new(
    data: data,
    archive_path: archive_path,
    **options,
  )

  @entries << entry
  entry
end

#add_file(source_path, archive_path = nil, **options) ⇒ FileEntry

Add file from disk

Parameters:

  • source_path (String)

    Path to source file

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

    Path in archive (nil = use basename)

  • options (Hash)

    Format-specific options

Returns:

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cabriolet/file_manager.rb', line 32

def add_file(source_path, archive_path = nil, **options)
  archive_path ||= File.basename(source_path)

  entry = FileEntry.new(
    source: source_path,
    archive_path: archive_path,
    **options,
  )

  @entries << entry
  entry
end

#allArray<FileEntry>

Get all entries

Returns:

  • (Array<FileEntry>)

    Copy of entries array



95
96
97
# File 'lib/cabriolet/file_manager.rb', line 95

def all
  @entries.dup
end

#clearself

Clear all entries

Returns:

  • (self)


102
103
104
105
# File 'lib/cabriolet/file_manager.rb', line 102

def clear
  @entries.clear
  self
end

#disk_filesArray<FileEntry>

Get files from disk

Returns:



117
118
119
# File 'lib/cabriolet/file_manager.rb', line 117

def disk_files
  @entries.select(&:from_disk?)
end

#each {|FileEntry| ... } ⇒ Object

Enumerate entries (Enumerable interface)

Yields:



65
66
67
# File 'lib/cabriolet/file_manager.rb', line 65

def each(&)
  @entries.each(&)
end

#empty?Boolean

Check if empty

Returns:

  • (Boolean)

    true if no files added



72
73
74
# File 'lib/cabriolet/file_manager.rb', line 72

def empty?
  @entries.empty?
end

#find_by_path(path) ⇒ FileEntry?

Find entry by archive path

Parameters:

  • path (String)

    Archive path to find

Returns:

  • (FileEntry, nil)

    Entry or nil if not found



132
133
134
# File 'lib/cabriolet/file_manager.rb', line 132

def find_by_path(path)
  @entries.find { |entry| entry.archive_path == path }
end

#memory_filesArray<FileEntry>

Get files from memory

Returns:

  • (Array<FileEntry>)

    Memory-based entries



124
125
126
# File 'lib/cabriolet/file_manager.rb', line 124

def memory_files
  @entries.select(&:from_memory?)
end

#path_exists?(path) ⇒ Boolean

Check if archive path exists

Parameters:

  • path (String)

    Archive path to check

Returns:

  • (Boolean)

    true if path exists



140
141
142
# File 'lib/cabriolet/file_manager.rb', line 140

def path_exists?(path)
  !find_by_path(path).nil?
end

#sizeInteger Also known as: count

Get count of entries

Returns:

  • (Integer)

    Number of entries



79
80
81
# File 'lib/cabriolet/file_manager.rb', line 79

def size
  @entries.size
end

#total_sizeInteger

Calculate total size of all files

Returns:

  • (Integer)

    Total size in bytes



110
111
112
# File 'lib/cabriolet/file_manager.rb', line 110

def total_size
  @entries.sum(&:size)
end