Class: Omnizip::Algorithm
- Inherits:
-
Object
- Object
- Omnizip::Algorithm
- Defined in:
- lib/omnizip/algorithm.rb
Overview
Abstract base class for compression algorithms.
All compression algorithms should inherit from this class and implement the required methods. Algorithms are automatically registered with the AlgorithmRegistry when defined.
Direct Known Subclasses
Omnizip::Algorithms::BZip2, Omnizip::Algorithms::Deflate, Omnizip::Algorithms::Deflate64, Omnizip::Algorithms::LZMA, Omnizip::Algorithms::LZMA2, Omnizip::Algorithms::PPMd7, Omnizip::Algorithms::PPMdBase, Omnizip::Algorithms::SevenZipLZMA2, Omnizip::Algorithms::XZLZMA2, Omnizip::Algorithms::Zstandard
Instance Attribute Summary collapse
-
#filter ⇒ Object
readonly
Returns the value of attribute filter.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.compress(data, **options) ⇒ String
Class-level convenience: compress
dataand return the compressed bytes. -
.decompress(data, **options) ⇒ String
Class-level convenience: decompress
dataand return the uncompressed bytes. -
.inherited(subclass) ⇒ void
Automatically register algorithm when inherited.
-
.metadata ⇒ Models::AlgorithmMetadata
Get metadata about this algorithm.
Instance Method Summary collapse
-
#compress(input, output) ⇒ void
Compress data from input to output.
-
#decompress(input, output) ⇒ void
Decompress data from input to output.
-
#initialize(options = {}) ⇒ Algorithm
constructor
Initialize algorithm with options.
-
#with_filter(filter) ⇒ self
Set a preprocessing filter for this algorithm.
Constructor Details
#initialize(options = {}) ⇒ Algorithm
Initialize algorithm with options.
33 34 35 36 |
# File 'lib/omnizip/algorithm.rb', line 33 def initialize( = {}) @options = @filter = nil end |
Instance Attribute Details
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
28 29 30 |
# File 'lib/omnizip/algorithm.rb', line 28 def filter @filter end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
28 29 30 |
# File 'lib/omnizip/algorithm.rb', line 28 def @options end |
Class Method Details
.compress(data, **options) ⇒ String
Class-level convenience: compress data and return the
compressed bytes. Carries the algorithm's streaming contract
over a single StringIO so callers with the whole payload in
memory (parallel compressors, small files) don't need to
instantiate and wire streams themselves.
96 97 98 99 100 101 102 103 |
# File 'lib/omnizip/algorithm.rb', line 96 def compress(data, **) instance = new() input = data.is_a?(::IO) ? data : ::StringIO.new(data.to_s.b) output = ::StringIO.new output.set_encoding(Encoding::BINARY) instance.compress(input, output, ) output.string end |
.decompress(data, **options) ⇒ String
Class-level convenience: decompress data and return the
uncompressed bytes. Mirror of .compress.
111 112 113 114 115 116 117 118 |
# File 'lib/omnizip/algorithm.rb', line 111 def decompress(data, **) instance = new() input = data.is_a?(::IO) ? data : ::StringIO.new(data.to_s.b) output = ::StringIO.new output.set_encoding(Encoding::BINARY) instance.decompress(input, output, ) output.string end |
.inherited(subclass) ⇒ void
This method returns an undefined value.
Automatically register algorithm when inherited.
This hook is called whenever a class inherits from Algorithm, automatically registering it with the AlgorithmRegistry.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/omnizip/algorithm.rb', line 127 def inherited(subclass) super # Register algorithm when metadata is defined subclass.define_singleton_method(:register_algorithm) do = subclass. AlgorithmRegistry.register(.name.to_sym, subclass) rescue NotImplementedError # Metadata not yet defined, will be registered manually end end |
.metadata ⇒ Models::AlgorithmMetadata
Get metadata about this algorithm.
82 83 84 85 |
# File 'lib/omnizip/algorithm.rb', line 82 def raise NotImplementedError, "#{self} must implement .metadata" end |
Instance Method Details
#compress(input, output) ⇒ void
This method returns an undefined value.
Compress data from input to output.
If a filter is set, data is filtered before compression.
59 60 61 62 |
# File 'lib/omnizip/algorithm.rb', line 59 def compress(input, output) raise NotImplementedError, "#{self.class} must implement #compress" end |
#decompress(input, output) ⇒ void
This method returns an undefined value.
Decompress data from input to output.
If a filter is set, data is unfiltered after decompression.
72 73 74 75 |
# File 'lib/omnizip/algorithm.rb', line 72 def decompress(input, output) raise NotImplementedError, "#{self.class} must implement #decompress" end |
#with_filter(filter) ⇒ self
Set a preprocessing filter for this algorithm.
The filter will be applied before compression and reversed after decompression. Returns self for method chaining.
46 47 48 49 |
# File 'lib/omnizip/algorithm.rb', line 46 def with_filter(filter) @filter = filter self end |