Class: Omnizip::FilterRegistry

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

Constant Summary collapse

DEFAULT_FORMATS =
%i[xz seven_zip].freeze
BUILTIN_FILTERS =
{
  "bcj-x86": "Omnizip::Filters::BcjX86",
  "bcj-arm": "Omnizip::Filters::BcjArm",
  "bcj-arm64": "Omnizip::Filters::BcjArm64",
  "bcj-ia64": "Omnizip::Filters::BcjIa64",
  "bcj-ppc": "Omnizip::Filters::BcjPpc",
  "bcj-sparc": "Omnizip::Filters::BcjSparc",
  bcj: "Omnizip::Filters::BCJ",
  bcj2: "Omnizip::Filters::Bcj2",
  delta: "Omnizip::Filters::Delta",
}.freeze

Class Method Summary collapse

Class Method Details

.filters_for_format(format) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/omnizip/filter_registry.rb', line 72

def filters_for_format(format)
  # Ensure all lazy triggers have fired so the format filter list
  # reflects every registered builtin.
  lazy_triggers.keys.dup.each { |name| get(name) }

  storage.select { |_, info| info[:formats]&.include?(format) }.keys
end

.get(name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/omnizip/filter_registry.rb', line 42

def get(name)
  entry = entry_for(name)
  unless entry
    raise not_found_error_class,
          "#{label} not registered: #{name.inspect}. " \
          "Available: #{available.join(', ')}"
  end

  entry[:class]
end

.get_for_format(name, format) ⇒ Object

Raises:

  • (KeyError)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/omnizip/filter_registry.rb', line 53

def get_for_format(name, format)
  entry = entry_for(name)
  raise KeyError, "Filter not found: #{name}" unless entry

  unless entry[:formats].include?(format)
    raise ArgumentError,
          "Filter #{name} not supported for format #{format}"
  end

  entry[:class].new
end

.labelObject



24
25
26
# File 'lib/omnizip/filter_registry.rb', line 24

def label
  "Filter"
end

.not_found_error_classObject



20
21
22
# File 'lib/omnizip/filter_registry.rb', line 20

def not_found_error_class
  Omnizip::UnknownFilterError
end

.register(name, filter_class, formats: DEFAULT_FORMATS) ⇒ Object Also known as: register_with_formats

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/omnizip/filter_registry.rb', line 28

def register(name, filter_class, formats: DEFAULT_FORMATS)
  raise ArgumentError, "Filter name cannot be nil" if name.nil?
  raise ArgumentError, "Filter class cannot be nil" if filter_class.nil?

  synchronize do
    storage[normalize_key(name)] = {
      class: filter_class,
      formats: formats,
    }
  end
  filter_class
end

.supports_format?(name, format) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'lib/omnizip/filter_registry.rb', line 65

def supports_format?(name, format)
  entry = entry_for(name)
  return false unless entry

  entry[:formats]&.include?(format)
end