Class: Cabriolet::AlgorithmFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/algorithm_factory.rb

Overview

Factory for creating and managing compression/decompression algorithms

The AlgorithmFactory provides a centralized registry for compression and decompression algorithms. It handles algorithm registration, validation, instantiation, and type normalization.

Examples:

Register and create an algorithm

factory = AlgorithmFactory.new
factory.register(:custom, CustomCompressor, category: :compressor)
algorithm = factory.create(:custom, :compressor, io, input, output, 4096)

Use with integer type constants

# Constants::COMP_TYPE_MSZIP (1) is normalized to :mszip
algorithm = factory.create(1, :decompressor, io, input, output, 4096)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auto_register: true) ⇒ AlgorithmFactory

Initialize a new algorithm factory

Parameters:

  • auto_register (Boolean) (defaults to: true)

    Whether to automatically register built-in algorithms



26
27
28
29
# File 'lib/cabriolet/algorithm_factory.rb', line 26

def initialize(auto_register: true)
  @algorithms = { compressor: {}, decompressor: {} }
  register_built_in_algorithms if auto_register
end

Instance Attribute Details

#algorithmsHash (readonly)

Returns Registry of algorithms by category and type.

Returns:

  • (Hash)

    Registry of algorithms by category and type



20
21
22
# File 'lib/cabriolet/algorithm_factory.rb', line 20

def algorithms
  @algorithms
end

Instance Method Details

#create(type, category, io_system, input, output, buffer_size, **kwargs) ⇒ Compressors::Base, Decompressors::Base

Create an instance of a registered algorithm

Examples:

Create a decompressor

decompressor = factory.create(:mszip, :decompressor,
                              io, input, output, 4096)

Create with integer constant

# Constants::COMP_TYPE_LZX (3) -> :lzx
compressor = factory.create(3, :compressor,
                            io, input, output, 8192)

Parameters:

Returns:

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cabriolet/algorithm_factory.rb', line 93

def create(type, category, io_system, input, output, buffer_size,
           **kwargs)
  validate_category!(category)

  normalized_type = normalize_type(type)
  algorithm_info = @algorithms[category][normalized_type]

  unless algorithm_info
    raise UnsupportedFormatError,
          "Unknown #{category} algorithm: #{normalized_type}"
  end

  algorithm_info[:class].new(io_system, input, output, buffer_size,
                             **kwargs)
end

#list(category = nil) ⇒ Hash

List registered algorithms

Examples:

List all algorithms

factory.list
#=> { compressor: { mszip: {...}, lzx: {...} },
#     decompressor: { none: {...}, mszip: {...} } }

List compressors only

factory.list(:compressor)
#=> { mszip: {...}, lzx: {...}, quantum: {...}, lzss: {...} }

Parameters:

  • category (Symbol, nil) (defaults to: nil)

    Optional category filter

Returns:

  • (Hash)

    Hash of registered algorithms



137
138
139
140
141
142
143
144
145
146
# File 'lib/cabriolet/algorithm_factory.rb', line 137

def list(category = nil)
  if category.nil?
    {
      compressor: @algorithms[:compressor].dup,
      decompressor: @algorithms[:decompressor].dup,
    }
  else
    @algorithms[category]&.dup || {}
  end
end

#register(type, algorithm_class, **options) ⇒ self

Register an algorithm in the factory

Examples:

Register a custom compressor

factory.register(:custom, MyCompressor,
                 category: :compressor, priority: 10)

Chain multiple registrations

factory
  .register(:algo1, Algo1, category: :compressor)
  .register(:algo2, Algo2, category: :decompressor)

Parameters:

  • type (Symbol)

    Algorithm type (:none, :mszip, :lzx, :quantum, :lzss)

  • algorithm_class (Class)

    Algorithm class (must inherit from Compressors::Base or Decompressors::Base)

  • options (Hash)

    Registration options

Options Hash (**options):

  • :category (Symbol)

    Required - :compressor or :decompressor

  • :priority (Integer)

    Priority for selection (default: 0)

  • :format (Symbol, nil)

    Format restriction (optional)

Returns:

  • (self)

    Returns self for method chaining

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cabriolet/algorithm_factory.rb', line 56

def register(type, algorithm_class, **options)
  category = options[:category]
  validate_category!(category)
  validate_algorithm_class!(algorithm_class, category)

  @algorithms[category][type] = {
    class: algorithm_class,
    priority: options.fetch(:priority, 0),
    format: options[:format],
  }

  self
end

#registered?(type, category) ⇒ Boolean

Check if an algorithm is registered

Examples:

Check registration

factory.registered?(:mszip, :compressor) #=> true
factory.registered?(:unknown, :compressor) #=> false

Parameters:

  • type (Symbol)

    Algorithm type

  • category (Symbol)

    Category (:compressor or :decompressor)

Returns:

  • (Boolean)

    True if registered, false otherwise



119
120
121
# File 'lib/cabriolet/algorithm_factory.rb', line 119

def registered?(type, category)
  @algorithms[category]&.key?(type) || false
end

#unregister(type, category) ⇒ Boolean

Unregister an algorithm

rubocop:disable Naming/PredicatePrefix

Examples:

Unregister an algorithm

factory.unregister(:mszip, :compressor) #=> true
factory.unregister(:unknown, :compressor) #=> false

Parameters:

  • type (Symbol)

    Algorithm type to remove

  • category (Symbol)

    Category (:compressor or :decompressor)

Returns:

  • (Boolean)

    True if removed, false if not found



159
160
161
# File 'lib/cabriolet/algorithm_factory.rb', line 159

def unregister(type, category)
  !@algorithms[category].delete(type).nil?
end