Class: Cabriolet::AlgorithmFactory
- Inherits:
-
Object
- Object
- Cabriolet::AlgorithmFactory
- 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.
Instance Attribute Summary collapse
-
#algorithms ⇒ Hash
readonly
Registry of algorithms by category and type.
Instance Method Summary collapse
-
#create(type, category, io_system, input, output, buffer_size, **kwargs) ⇒ Compressors::Base, Decompressors::Base
Create an instance of a registered algorithm.
-
#initialize(auto_register: true) ⇒ AlgorithmFactory
constructor
Initialize a new algorithm factory.
-
#list(category = nil) ⇒ Hash
List registered algorithms.
-
#register(type, algorithm_class, **options) ⇒ self
Register an algorithm in the factory.
-
#registered?(type, category) ⇒ Boolean
Check if an algorithm is registered.
-
#unregister(type, category) ⇒ Boolean
Unregister an algorithm.
Constructor Details
#initialize(auto_register: true) ⇒ AlgorithmFactory
Initialize a new algorithm factory
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
#algorithms ⇒ Hash (readonly)
Returns 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
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
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
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, **) category = [:category] validate_category!(category) validate_algorithm_class!(algorithm_class, category) @algorithms[category][type] = { class: algorithm_class, priority: .fetch(:priority, 0), format: [:format], } self end |
#registered?(type, category) ⇒ Boolean
Check if an algorithm is registered
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
159 160 161 |
# File 'lib/cabriolet/algorithm_factory.rb', line 159 def unregister(type, category) !@algorithms[category].delete(type).nil? end |