Class: Cabriolet::BaseCompressor
- Inherits:
-
Object
- Object
- Cabriolet::BaseCompressor
- Defined in:
- lib/cabriolet/base_compressor.rb
Overview
Abstract base class for all format compressors
Implements Template Method pattern:
-
Defines common compression workflow in generate()
-
Subclasses implement format-specific hooks
Provides:
-
File management via FileManager
-
Common initialization pattern
-
Template method for generation workflow
-
Hook methods for format customization
-
Helper methods for common operations
Subclasses must implement:
-
build_structure(options) - Create format-specific structure
-
write_format(output_handle, structure) - Write binary data
Subclasses may override:
-
validate_generation_prerequisites!(options) - Custom validation
-
post_generation_hook(output_file, structure, bytes) - Cleanup/logging
Instance Attribute Summary collapse
-
#algorithm_factory ⇒ Object
readonly
Returns the value of attribute algorithm_factory.
-
#file_manager ⇒ Object
readonly
Returns the value of attribute file_manager.
-
#io_system ⇒ Object
readonly
Returns the value of attribute io_system.
Instance Method Summary collapse
-
#add_data(data, archive_path, **options) ⇒ FileEntry
Add file from memory to archive.
-
#add_file(source_path, archive_path = nil, **options) ⇒ FileEntry
Add file from disk to archive.
-
#generate(output_file, **options) ⇒ Integer
Generate archive (Template Method).
-
#initialize(io_system = nil, algorithm_factory = nil) ⇒ BaseCompressor
constructor
Initialize compressor with I/O and algorithm dependencies.
Constructor Details
#initialize(io_system = nil, algorithm_factory = nil) ⇒ BaseCompressor
Initialize compressor with I/O and algorithm dependencies
47 48 49 50 51 |
# File 'lib/cabriolet/base_compressor.rb', line 47 def initialize(io_system = nil, algorithm_factory = nil) @io_system = io_system || System::IOSystem.new @algorithm_factory = algorithm_factory || Cabriolet.algorithm_factory @file_manager = FileManager.new end |
Instance Attribute Details
#algorithm_factory ⇒ Object (readonly)
Returns the value of attribute algorithm_factory.
41 42 43 |
# File 'lib/cabriolet/base_compressor.rb', line 41 def algorithm_factory @algorithm_factory end |
#file_manager ⇒ Object (readonly)
Returns the value of attribute file_manager.
41 42 43 |
# File 'lib/cabriolet/base_compressor.rb', line 41 def file_manager @file_manager end |
#io_system ⇒ Object (readonly)
Returns the value of attribute io_system.
41 42 43 |
# File 'lib/cabriolet/base_compressor.rb', line 41 def io_system @io_system end |
Instance Method Details
#add_data(data, archive_path, **options) ⇒ FileEntry
Add file from memory to archive
70 71 72 |
# File 'lib/cabriolet/base_compressor.rb', line 70 def add_data(data, archive_path, **) @file_manager.add_data(data, archive_path, **) end |
#add_file(source_path, archive_path = nil, **options) ⇒ FileEntry
Add file from disk to archive
60 61 62 |
# File 'lib/cabriolet/base_compressor.rb', line 60 def add_file(source_path, archive_path = nil, **) @file_manager.add_file(source_path, archive_path, **) end |
#generate(output_file, **options) ⇒ Integer
Generate archive (Template Method)
This method defines the compression workflow:
-
Validate prerequisites
-
Build format-specific structure
-
Write to output file
-
Post-generation hook
-
Return bytes written
Subclasses customize via hook methods, not by overriding this method.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/cabriolet/base_compressor.rb', line 89 def generate(output_file, **) validate_generation_prerequisites!() structure = build_structure() bytes_written = write_to_file(output_file, structure) post_generation_hook(output_file, structure, bytes_written) bytes_written end |