Class: Distillery::Archiver Abstract
- Inherits:
-
Object
- Object
- Distillery::Archiver
- Includes:
- Enumerable
- Defined in:
- lib/distillery/archiver.rb,
lib/distillery/archiver/zip.rb,
lib/distillery/archiver/archive.rb,
lib/distillery/archiver/external.rb,
lib/distillery/archiver/libarchive.rb
Overview
Allow processing of archives
Direct Known Subclasses
Defined Under Namespace
Classes: Archive, ArchiverNotFound, Error, ExecError, External, InputStream, LibArchive, OutputStream, ProcessingError, Zip
Class Method Summary collapse
-
.add(provider) ⇒ self
Add an archiver provider class.
-
.archivers ⇒ Array<Archiver>
List of registered archivers.
-
.for(file) ⇒ Object
Return an archive instance of the specified file, or invokes the block with the archive instance passed as parameter.
-
.for_extension(extension) ⇒ Object
Archiver able to process the selected extension.
-
.for_file(file) ⇒ Archiver?
Archiver able to process the selected file.
-
.for_mimetype(mimetype) ⇒ Object
Archiver able to process the selected mimetype.
-
.logger ⇒ Logger?
Get the regisered logger.
-
.logger=(logger) ⇒ Object
Register a logger.
-
.providers ⇒ Array<Class>
List of archivers’ providers in loading order.
-
.register(archiver, warnings: true) ⇒ Object
Register an archiver.
-
.registering ⇒ self
Perform automatic registration of all the archive providers.
Instance Method Summary collapse
-
#delete!(file, entry) ⇒ Boolean
Delete the entry from the archive.
-
#each(file) {|entry, io| ... } ⇒ self, Enumerator
Iterate over each archive entry.
-
#empty?(file) ⇒ Boolean
Check if the archive contains no entry.
-
#entries(file) ⇒ Array<String>
List archive entries.
-
#extensions ⇒ Array<String>
List of supported extensions.
-
#initialize ⇒ Archiver
constructor
A new instance of Archiver.
-
#mimetypes ⇒ Array<String>
List of supported mimetypes.
-
#reader(file, entry) {|io| ... } ⇒ Object
Allow to perform read operation on an archive entry.
-
#writer(file, entry) {|io| ... } ⇒ Object
Allow to perform write operation on an archive entry.
Constructor Details
#initialize ⇒ Archiver
Returns a new instance of Archiver.
271 272 273 |
# File 'lib/distillery/archiver.rb', line 271 def initialize raise "abstract class" end |
Class Method Details
.add(provider) ⇒ self
Add an archiver provider class
112 113 114 115 |
# File 'lib/distillery/archiver.rb', line 112 def self.add(provider) @@providers << provider self end |
.archivers ⇒ Array<Archiver>
List of registered archivers
187 188 189 |
# File 'lib/distillery/archiver.rb', line 187 def self.archivers @@archivers.to_a end |
.for(file) ⇒ Archive .for(file) {|archive| ... } ⇒ self
Return an archive instance of the specified file, or invokes the block with the archive instance passed as parameter.
257 258 259 260 261 262 263 264 265 |
# 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) ⇒ Object
Archiver able to process the selected extension
201 202 203 204 |
# 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) ⇒ Archiver?
Archiver able to process the selected file.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# 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) ⇒ Object
Archiver able to process the selected mimetype
194 195 196 |
# File 'lib/distillery/archiver.rb', line 194 def self.for_mimetype(mimetype) @@mimetypes[mimetype] end |
.logger ⇒ Logger?
Get the regisered logger
101 102 103 |
# File 'lib/distillery/archiver.rb', line 101 def self.logger @@logger end |
.logger=(logger) ⇒ Object
Register a logger
92 93 94 |
# File 'lib/distillery/archiver.rb', line 92 def self.logger=(logger) @@logger = logger end |
.providers ⇒ Array<Class>
List of archivers’ providers in loading order.
122 123 124 125 126 127 |
# 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) ⇒ Object
Register an archiver.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# 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 ⇒ self
Perform automatic registration of all the archive providers
134 135 136 137 138 139 |
# File 'lib/distillery/archiver.rb', line 134 def self.registering self.providers.each do |p| p.registering end self end |
Instance Method Details
#delete!(file, entry) ⇒ Boolean
Delete the entry from the archive
368 369 370 |
# File 'lib/distillery/archiver.rb', line 368 def delete!(file, entry) false end |
#each(file) {|entry, io| ... } ⇒ self, Enumerator
Iterate over each archive entry
303 304 305 |
# File 'lib/distillery/archiver.rb', line 303 def each(file, &block) raise "abstract method" end |
#empty?(file) ⇒ Boolean
Check if the archive contains no entry
314 315 316 |
# File 'lib/distillery/archiver.rb', line 314 def empty?(file) ! each(file).any? end |
#entries(file) ⇒ Array<String>
List archive entries
325 326 327 |
# File 'lib/distillery/archiver.rb', line 325 def entries(file) each(file).map {|a_entry, _| a_entry } end |
#extensions ⇒ Array<String>
List of supported extensions
280 281 282 |
# File 'lib/distillery/archiver.rb', line 280 def extensions [ 'zip' ] end |
#mimetypes ⇒ Array<String>
List of supported mimetypes
289 290 291 |
# File 'lib/distillery/archiver.rb', line 289 def mimetypes [ 'application/zip' ] end |
#reader(file, entry) {|io| ... } ⇒ Object
Allow to perform read operation on an archive entry
339 340 341 342 343 344 |
# 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) {|io| ... } ⇒ Object
Allow to perform write operation on an archive entry
356 357 358 |
# File 'lib/distillery/archiver.rb', line 356 def writer(file, entry, &block) nil end |