Class: Omnizip::FormatRegistry

Inherits:
Registry
  • Object
show all
Defined in:
lib/omnizip/format_registry.rb

Constant Summary collapse

BUILTIN_FORMATS =
{
  ".7z" => "Omnizip::Formats::SevenZip::Reader",
  ".zip" => "Omnizip::Formats::Zip",
  ".rar" => "Omnizip::Formats::Rar::Reader",
  ".tar" => "Omnizip::Formats::Tar",
  ".gz" => "Omnizip::Formats::Gzip",
  ".gzip" => "Omnizip::Formats::Gzip",
  ".bz2" => "Omnizip::Formats::Bzip2File",
  ".bzip2" => "Omnizip::Formats::Bzip2File",
  ".xz" => "Omnizip::Formats::Xz",
  ".msi" => "Omnizip::Formats::Msi",
  ".msp" => "Omnizip::Formats::Msi",
  ".cpio" => "Omnizip::Formats::Cpio::Reader",
  ".iso" => "Omnizip::Formats::Iso::Reader",
  ".xar" => "Omnizip::Formats::Xar::Reader",
  ".lz" => "Omnizip::Formats::Lzip",
  ".lzip" => "Omnizip::Formats::Lzip",
  ".lzma" => "Omnizip::Formats::LzmaAlone",
  ".ole" => "Omnizip::Formats::Ole::Storage",
  ".doc" => "Omnizip::Formats::Ole::Storage",
  ".xls" => "Omnizip::Formats::Ole::Storage",
  ".ppt" => "Omnizip::Formats::Ole::Storage",
}.freeze

Class Method Summary collapse

Class Method Details

.get(extension) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/omnizip/format_registry.rb', line 40

def get(extension)
  normalized = normalize_key(extension)
  handler = storage[normalized]
  return resolve_handler(handler, extension) if handler

  trigger = lazy_triggers[normalized]
  if trigger
    synchronize { lazy_triggers.delete(normalized) }
    trigger.call
    handler = storage[normalized]
    return resolve_handler(handler, extension) if handler
  end

  nil
end

.labelObject



30
31
32
# File 'lib/omnizip/format_registry.rb', line 30

def label
  "Format"
end

.normalize_key(extension) ⇒ Object



34
35
36
37
38
# File 'lib/omnizip/format_registry.rb', line 34

def normalize_key(extension)
  ext = extension.to_s
  ext = ".#{ext}" unless ext.start_with?(".")
  ext.downcase
end

.resolve_constant(name) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/omnizip/format_registry.rb', line 61

def resolve_constant(name)
  return nil unless name.is_a?(String)

  parts = name.split("::")
  parts.shift if parts.empty? || parts.first.empty?

  constant = Object
  parts.each do |n|
    return nil unless constant.const_defined?(n, false)

    constant = constant.const_get(n)
  end
  constant
rescue StandardError
  nil
end

.supported?(extension) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/omnizip/format_registry.rb', line 56

def supported?(extension)
  registered?(extension) || lazy_triggers.key?(normalize_key(extension))
end