Class: Omnizip::Algorithm

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Algorithm

Initialize algorithm with options.

Parameters:

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

    Algorithm-specific options



33
34
35
36
# File 'lib/omnizip/algorithm.rb', line 33

def initialize(options = {})
  @options = options
  @filter = nil
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



28
29
30
# File 'lib/omnizip/algorithm.rb', line 28

def filter
  @filter
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/omnizip/algorithm.rb', line 28

def options
  @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.

Parameters:

  • data (String, IO)

    Input to compress

  • options (Hash)

    Forwarded to the algorithm

Returns:

  • (String)

    Compressed bytes



96
97
98
99
100
101
102
103
# File 'lib/omnizip/algorithm.rb', line 96

def compress(data, **options)
  instance = new(options)
  input = data.is_a?(::IO) ? data : ::StringIO.new(data.to_s.b)
  output = ::StringIO.new
  output.set_encoding(Encoding::BINARY)
  instance.compress(input, output, options)
  output.string
end

.decompress(data, **options) ⇒ String

Class-level convenience: decompress data and return the uncompressed bytes. Mirror of .compress.

Parameters:

  • data (String, IO)

    Input to decompress

  • options (Hash)

    Forwarded to the algorithm

Returns:

  • (String)

    Decompressed bytes



111
112
113
114
115
116
117
118
# File 'lib/omnizip/algorithm.rb', line 111

def decompress(data, **options)
  instance = new(options)
  input = data.is_a?(::IO) ? data : ::StringIO.new(data.to_s.b)
  output = ::StringIO.new
  output.set_encoding(Encoding::BINARY)
  instance.decompress(input, output, options)
  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.

Parameters:

  • subclass (Class)

    The inheriting class



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
    meta = subclass.
    AlgorithmRegistry.register(meta.name.to_sym, subclass)
  rescue NotImplementedError
    # Metadata not yet defined, will be registered manually
  end
end

.metadataModels::AlgorithmMetadata

Get metadata about this algorithm.

Returns:

Raises:

  • (NotImplementedError)

    Must be implemented by subclass



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.

Parameters:

  • input (IO, String, #read)

    Input source

  • output (IO, #write)

    Output destination

Raises:

  • (NotImplementedError)

    Must be implemented by subclass



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.

Parameters:

  • input (IO, String, #read)

    Input source

  • output (IO, #write)

    Output destination

Raises:

  • (NotImplementedError)

    Must be implemented by subclass



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.

Parameters:

Returns:

  • (self)

    For method chaining



46
47
48
49
# File 'lib/omnizip/algorithm.rb', line 46

def with_filter(filter)
  @filter = filter
  self
end