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



31
32
33
34
# File 'lib/omnizip/algorithm.rb', line 31

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

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



26
27
28
# File 'lib/omnizip/algorithm.rb', line 26

def filter
  @filter
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/omnizip/algorithm.rb', line 26

def options
  @options
end

Class Method Details

.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



92
93
94
95
96
97
98
99
100
101
# File 'lib/omnizip/algorithm.rb', line 92

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



80
81
82
83
# File 'lib/omnizip/algorithm.rb', line 80

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



57
58
59
60
# File 'lib/omnizip/algorithm.rb', line 57

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



70
71
72
73
# File 'lib/omnizip/algorithm.rb', line 70

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



44
45
46
47
# File 'lib/omnizip/algorithm.rb', line 44

def with_filter(filter)
  @filter = filter
  self
end