Class: Cabriolet::Collections::FileCollection
- Inherits:
-
Object
- Object
- Cabriolet::Collections::FileCollection
- 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
-
#add(source_path, archive_path = nil, **options) ⇒ self
Add a file to the collection.
-
#add_all(files) ⇒ self
Add multiple files at once.
-
#by_directory ⇒ Hash
Group files by directory for archive organization.
-
#clear ⇒ self
Clear all files from the collection.
-
#each {|file_entry| ... } ⇒ Enumerator
Iterate over files in the collection.
-
#empty? ⇒ Boolean
Check if collection is empty.
-
#find_by_pattern(pattern) ⇒ Array<Hash>
Find files by pattern in archive path.
-
#initialize(format_options = {}) ⇒ FileCollection
constructor
Initialize a new file collection.
-
#prepare_for_compression ⇒ Array<Hash>
Prepare files for compression by reading metadata.
-
#size ⇒ Integer
Get the number of files in the collection.
-
#total_size ⇒ Integer
Get total uncompressed size of all files.
Constructor Details
#initialize(format_options = {}) ⇒ FileCollection
Initialize a new file collection
13 14 15 16 |
# File 'lib/cabriolet/collections/file_collection.rb', line 13 def initialize( = {}) @files = [] @format_options = end |
Instance Method Details
#add(source_path, archive_path = nil, **options) ⇒ self
Add a file to the collection
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, **) validate_source(source_path) @files << { source: source_path, archive: archive_path || ::File.basename(source_path), options: , } self end |
#add_all(files) ⇒ self
Add multiple files at once
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_directory ⇒ Hash
Group files by directory for archive organization
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 |
#clear ⇒ self
Clear all files from the collection
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
55 56 57 |
# File 'lib/cabriolet/collections/file_collection.rb', line 55 def each(&) @files.each(&) end |
#empty? ⇒ Boolean
Check if collection is empty
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
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_compression ⇒ Array<Hash>
Prepare files for compression by reading metadata
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 |
#size ⇒ Integer
Get the number of files in the collection
62 63 64 |
# File 'lib/cabriolet/collections/file_collection.rb', line 62 def size @files.size end |
#total_size ⇒ Integer
Get total uncompressed size of all files
93 94 95 |
# File 'lib/cabriolet/collections/file_collection.rb', line 93 def total_size @files.sum { |f| ::File.size(f[:source]) } end |