Module: AESCompression
- Defined in:
- lib/compression.rb
Overview
Copyright © 2025 Tom Lahti MIT License
Constant Summary collapse
- ALGORITHM_IDS =
Compression algorithm identifiers for serialization
{ nil => 0, # No compression :zstd => 1, :snappy => 2, :lz4 => 3 }.freeze
- ID_TO_ALGORITHM =
ALGORITHM_IDS.invert.freeze
Class Method Summary collapse
- .algorithm_available?(algorithm) ⇒ Boolean
- .available_algorithms ⇒ Object
- .compress(data, algorithm = nil) ⇒ Object
- .decompress(data, algorithm) ⇒ Object
- .default_algorithm ⇒ Object
- .enabled? ⇒ Boolean
Class Method Details
.algorithm_available?(algorithm) ⇒ Boolean
83 84 85 |
# File 'lib/compression.rb', line 83 def self.algorithm_available?(algorithm) @algorithms.key?(algorithm) end |
.available_algorithms ⇒ Object
79 80 81 |
# File 'lib/compression.rb', line 79 def self.available_algorithms @algorithms.keys end |
.compress(data, algorithm = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/compression.rb', line 91 def self.compress(data, algorithm = nil) return [data, nil] unless data && !data.empty? algorithm ||= @default_algorithm return [data, nil] unless algorithm && @algorithms[algorithm] begin compressed = @algorithms[algorithm][:compress].call(data) [compressed, algorithm] rescue => e # Fallback to uncompressed data on error [data, nil] end end |
.decompress(data, algorithm) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/compression.rb', line 106 def self.decompress(data, algorithm) return data unless data && !data.empty? && algorithm # Check if algorithm is a valid symbol we recognize unless ID_TO_ALGORITHM.values.include?(algorithm) raise "Unknown compression algorithm identifier: #{algorithm}" end # Check if the algorithm is available unless @algorithms[algorithm] raise "Compression algorithm #{algorithm} required but not available. Please install the required gem." end begin @algorithms[algorithm][:decompress].call(data) rescue => e raise "Error decompressing data: #{e.}. The #{algorithm} library may not be installed correctly." end end |
.default_algorithm ⇒ Object
87 88 89 |
# File 'lib/compression.rb', line 87 def self.default_algorithm @default_algorithm end |
.enabled? ⇒ Boolean
126 127 128 |
# File 'lib/compression.rb', line 126 def self.enabled? !@algorithms.empty? end |