class Distillery::Archiver

@abstract Allow processing of archives

Public Class Methods

add(provider) click to toggle source

Add an archiver provider class

@param provider [Class] archiver provider

@return [self]

# File lib/distillery/archiver.rb, line 112
def self.add(provider)
    @@providers << provider
    self
end
archivers() click to toggle source

List of registered archivers

@return [Array<Archiver>]

# File lib/distillery/archiver.rb, line 187
def self.archivers
    @@archivers.to_a
end
for(file) { |archive| ... } click to toggle source

Return an archive instance of the specified file, or invokes the block with the archive instance passed as parameter.

@overload for(file)

@param file [String]             archive file

@raise [ArchiverNotFound]        an archiver able to process this file
                                 has not been found

@return [Archive]                archive instance

@overload for(file)

@param file [String]             archive file

@yieldparam archive [Archive]    archive instance

@raise [ArchiverNotFound]        an archiver able to process this file
                                 has not been found

@return [self]
# File lib/distillery/archiver.rb, line 257
def self.for(file)
    archive = Archive.new(file)
    if block_given?
        yield(archive)
        self
    else
        archive
    end
end
for_extension(extension) click to toggle source

Archiver able to process the selected extension

# File lib/distillery/archiver.rb, line 201
def self.for_extension(extension)
    extension = extension[1..-1] if extension[0] == '.'
    @@extensions[extension.downcase]
end
for_file(file) click to toggle source

Archiver able to process the selected file.

@param [String] file archive file tpo consider

@return [Archiver] archiver to use for processing file @return [nil] no matching archiver found

# File lib/distillery/archiver.rb, line 214
def self.for_file(file)
    # Find by extension
    parts      = File.basename(file).split('.')
    extlist    = 1.upto(parts.size-1).map {|i| parts[i..-1].join('.') }
    archiver   = extlist.lazy.map  {|ext| self.for_extension(ext) }
                             .find {|arc| ! arc.nil?              }

    # Find by mimetype if previously failed
    archiver ||= if defined?(MimeMagic)
                     begin
                         File.open(file) {|io|
                             self.for_mimetype(MimeMagic.by_magic(io))
                         }
                     rescue Errno::ENOENT
                     end
                 end

    # Return found archiver (or nil)
    archiver
end
for_mimetype(mimetype) click to toggle source

Archiver able to process the selected mimetype

# File lib/distillery/archiver.rb, line 194
def self.for_mimetype(mimetype)
    @@mimetypes[mimetype]
end
logger() click to toggle source

Get the regisered logger

@return [Logger,nil]

# File lib/distillery/archiver.rb, line 101
def self.logger
    @@logger
end
logger=(logger) click to toggle source

Register a logger

@param logger [Logger,nil] logger

@return logger

# File lib/distillery/archiver.rb, line 92
def self.logger=(logger)
    @@logger = logger
end
new() click to toggle source
# File lib/distillery/archiver.rb, line 271
def initialize
    raise "abstract class"
end
providers() click to toggle source

List of archivers' providers in loading order.

@return [Array<Class>] Archiver class

# File lib/distillery/archiver.rb, line 122
def self.providers
    # Could be done by looking at constants
    #   constants.lazy.map {|c| const_get(c) }.select {|k| k < self }.to_a
    # But we want to keep loading order
    @@providers
end
register(archiver, warnings: true) click to toggle source

Register an archiver.

@param [Archiver] archiver Archiver to register @param [Boolean] warnings Emit warning when overriding

# File lib/distillery/archiver.rb, line 148
def self.register(archiver, warnings: true)
    # Notifier
    notify = if warnings
                 ->(type, key, old, new) {
                     oname = old.class.name.split('::').last
                     nname = new.class.name.split('::').last
                     Archiver.logger&.warn {
                         "#{self} overriding #{type} for #{key}" \
                         " [#{oname} -> #{nname}]"
                     }
                 }
             end

    # Add archiver
    @@archivers.add(archiver)

    # Register mimetypes
    Array(archiver.mimetypes).each {|mt|
        @@mimetypes.merge!(mt => archiver) {|key, old, new|
            notify&.call("mimetype", key, old, new)
            new
        }
    }
    # Register extensions
    Array(archiver.extensions).each {|ext|
        @@extensions.merge!(ext.downcase => archiver) {|key, old, new|
            notify&.call("extension", key, old, new)
            new
        }
    }
    # Return archiver
    archiver
end
registering() click to toggle source

Perform automatic registration of all the archive providers

@return [self]

# File lib/distillery/archiver.rb, line 134
def self.registering
    self.providers.each do |p|
        p.registering
    end
    self
end

Public Instance Methods

delete!(file, entry) click to toggle source

Delete the entry from the archive

@param file [String] archive file @param entry [String] entry name

@return [Boolean] operation status

# File lib/distillery/archiver.rb, line 368
def delete!(file, entry)
    false
end
each(file, &block) click to toggle source

Iterate over each archive entry

@param file [String] archive file

@yieldparam entry [String] @yieldparam io [InputStream]

@return [self,Enumerator]

# File lib/distillery/archiver.rb, line 303
def each(file, &block)
    raise "abstract method"
end
empty?(file) click to toggle source

Check if the archive contains no entry

@param file [String] archive file

@return [Boolean]

# File lib/distillery/archiver.rb, line 314
def empty?(file)
    ! each(file).any?
end
entries(file) click to toggle source

List archive entries

@param file [String] archive file

@return [Array<String>]

# File lib/distillery/archiver.rb, line 325
def entries(file)
    each(file).map {|a_entry, _| a_entry }
end
extensions() click to toggle source

List of supported extensions

@return [Array<String>]

# File lib/distillery/archiver.rb, line 280
def extensions
    [ 'zip' ]
end
mimetypes() click to toggle source

List of supported mimetypes

@return [Array<String>]

# File lib/distillery/archiver.rb, line 289
def mimetypes
    [ 'application/zip' ]
end
reader(file, entry, &block) click to toggle source

Allow to perform read operation on an archive entry

@param file [String] archive file @param entry [String] entry name

@yieldparam io [InputStream] input stream for reading

@return block value

# File lib/distillery/archiver.rb, line 339
def reader(file, entry, &block)
    each(file) {|a_entry, a_io|
        next unless a_entry == entry
        return block.call(a_io)
    }
end
writer(file, entry, &block) click to toggle source

Allow to perform write operation on an archive entry

@param file [String] archive file @param entry [String] entry name

@yieldparam io [OutputStream] output stream for writing

@return block value

# File lib/distillery/archiver.rb, line 356
def writer(file, entry, &block)
    nil
end