Module: Omnizip::ArchiveHandler

Defined in:
lib/omnizip/archive_handler.rb

Overview

Unified archive handler interface for the Convenience module.

Each format that wants to participate in Omnizip.compress_file / extract_archive / etc. registers a Handler here. The Handler exposes a small canonical API (+create+, open, extract_to, list) that the Convenience module calls; the Handler then delegates to the format-specific reader/writer.

Examples:

Register a format

Omnizip::ArchiveHandler.register(:tar, TarHandler.new)

Use from a caller

Omnizip::ArchiveHandler.for(:tar).create(path) { |w| w.add_file(...) }

Class Method Summary collapse

Class Method Details

.availableArray<Symbol>

List of registered format names.

Returns:

  • (Array<Symbol>)


55
56
57
# File 'lib/omnizip/archive_handler.rb', line 55

def available
  @handlers.keys
end

.for(format) ⇒ Object

Look up the handler for format. Triggers the registered lazy load constant if the format is one of the built-ins.

Parameters:

  • format (Symbol)

Returns:

  • (Object)

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/omnizip/archive_handler.rb', line 36

def for(format)
  handler = @handlers[format.to_sym]
  return handler if handler

  trigger = LAZY_LOAD_TRIGGERS[format.to_sym]
  if trigger
    trigger.call
    handler = @handlers[format.to_sym]
    return handler if handler
  end

  raise Omnizip::UnsupportedFormatError,
        "No archive handler registered for #{format.inspect}. " \
        "Registered: #{@handlers.keys.join(', ')}"
end

.register(format, handler) ⇒ void

This method returns an undefined value.

Register a handler for a format symbol.

Parameters:

  • format (Symbol)

    e.g. :zip, :tar

  • handler (Object)

    responding to the Handler interface



26
27
28
# File 'lib/omnizip/archive_handler.rb', line 26

def register(format, handler)
  @handlers[format.to_sym] = handler
end

.unregister(format) ⇒ void

This method returns an undefined value.

Remove a handler (primarily for testing).



62
63
64
# File 'lib/omnizip/archive_handler.rb', line 62

def unregister(format)
  @handlers.delete(format.to_sym)
end