Class: Cabriolet::Collections::FileCollection

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

Overview

FileCollection manages a collection of files for compression Provides unified interface for adding files and preparing them for compression

Instance Method Summary collapse

Constructor Details

#initialize(format_options = {}) ⇒ FileCollection

Initialize a new file collection

Parameters:

  • format_options (Hash) (defaults to: {})

    Options specific to the archive format



13
14
15
16
# File 'lib/cabriolet/collections/file_collection.rb', line 13

def initialize(format_options = {})
  @files = []
  @format_options = format_options
end

Instance Method Details

#add(source_path, archive_path = nil, **options) ⇒ self

Add a file to the collection

Examples:

collection.add("README.md", "docs/README.md")
collection.add("data.txt") # Uses basename

Parameters:

  • source_path (String)

    Path to the source file

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

    Path within the archive (defaults to basename)

  • options (Hash)

    Additional options for this file

Returns:

  • (self)

    Returns self for chaining



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cabriolet/collections/file_collection.rb', line 28

def add(source_path, archive_path = nil, **options)
  validate_source(source_path)

  @files << {
    source: source_path,
    archive: archive_path || ::File.basename(source_path),
    options: options,
  }

  self
end

#add_all(files) ⇒ self

Add multiple files at once

Parameters:

  • files (Array<Hash>)

    Array of file hashes with :source, :archive, :options keys

Returns:

  • (self)

    Returns self for chaining



44
45
46
47
48
49
# File 'lib/cabriolet/collections/file_collection.rb', line 44

def add_all(files)
  files.each do |file|
    add(file[:source], file[:archive], **file.fetch(:options, {}))
  end
  self
end

#by_directoryHash

Group files by directory for archive organization

Returns:

  • (Hash)

    Hash with directory paths as keys and file arrays as values



100
101
102
103
104
# File 'lib/cabriolet/collections/file_collection.rb', line 100

def by_directory
  @files.group_by do |file|
    ::File.dirname(file[:archive])
  end
end

#clearself

Clear all files from the collection

Returns:

  • (self)

    Returns self for chaining



76
77
78
79
# File 'lib/cabriolet/collections/file_collection.rb', line 76

def clear
  @files.clear
  self
end

#each {|file_entry| ... } ⇒ Enumerator

Iterate over files in the collection

Yields:

  • (file_entry)

    Yields each file entry hash

Returns:

  • (Enumerator)

    If no block given



55
56
57
# File 'lib/cabriolet/collections/file_collection.rb', line 55

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

#empty?Boolean

Check if collection is empty

Returns:

  • (Boolean)

    True if no files



69
70
71
# File 'lib/cabriolet/collections/file_collection.rb', line 69

def empty?
  @files.empty?
end

#find_by_pattern(pattern) ⇒ Array<Hash>

Find files by pattern in archive path

Parameters:

  • pattern (String, Regexp)

    Pattern to match

Returns:

  • (Array<Hash>)

    Matching file entries



110
111
112
113
114
115
116
117
118
# File 'lib/cabriolet/collections/file_collection.rb', line 110

def find_by_pattern(pattern)
  @files.select do |file|
    if pattern.is_a?(Regexp)
      file[:archive] =~ pattern
    else
      file[:archive].include?(pattern)
    end
  end
end

#prepare_for_compressionArray<Hash>

Prepare files for compression by reading metadata

Returns:

  • (Array<Hash>)

    Array of prepared file info hashes



84
85
86
87
88
# File 'lib/cabriolet/collections/file_collection.rb', line 84

def prepare_for_compression
  @files.map do |file_entry|
    prepare_file_info(file_entry)
  end
end

#sizeInteger

Get the number of files in the collection

Returns:

  • (Integer)

    Number of files



62
63
64
# File 'lib/cabriolet/collections/file_collection.rb', line 62

def size
  @files.size
end

#total_sizeInteger

Get total uncompressed size of all files

Returns:

  • (Integer)

    Total size in bytes



93
94
95
# File 'lib/cabriolet/collections/file_collection.rb', line 93

def total_size
  @files.sum { |f| ::File.size(f[:source]) }
end