2019-10-03 09:19:35 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Compression
|
|
|
|
class Engine
|
|
|
|
UnsupportedFileExtension = Class.new(StandardError)
|
|
|
|
|
|
|
|
def self.default_strategies
|
|
|
|
[
|
|
|
|
Compression::Zip.new,
|
|
|
|
Compression::Pipeline.new([Compression::Tar.new, Compression::Gzip.new]),
|
|
|
|
Compression::Gzip.new,
|
|
|
|
Compression::Tar.new
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.engine_for(filename, strategies: default_strategies)
|
|
|
|
strategy = strategies.detect(-> { raise UnsupportedFileExtension }) { |e| e.can_handle?(filename) }
|
|
|
|
new(strategy)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(strategy)
|
|
|
|
@strategy = strategy
|
|
|
|
end
|
|
|
|
|
2019-10-09 10:41:16 -04:00
|
|
|
delegate :extension, :decompress, :compress, :strip_directory, to: :@strategy
|
2019-10-03 09:19:35 -04:00
|
|
|
end
|
|
|
|
end
|