Class: Omnizip::Formats::FormatSpecLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/formats/format_spec_loader.rb

Overview

Loads and manages format specifications from YAML configuration files

This class provides a configuration-driven architecture for archive formats, allowing format specifications to be externalized in YAML files rather than hardcoded in the application.

Examples:

Loading a format specification

spec = FormatSpecLoader.load("rar5")
spec.format.name # => "RAR5"
spec.format.magic_bytes # => [0x52, 0x61, 0x72, ...]

Getting all loaded specifications

specs = FormatSpecLoader.all_specs
specs.keys # => ["rar3", "rar5", "zip", ...]

Class Method Summary collapse

Class Method Details

.all_specsHash<String, FormatSpecification>

Get all loaded format specifications

Returns:



63
64
65
# File 'lib/omnizip/formats/format_spec_loader.rb', line 63

def all_specs
  @all_specs ||= {}
end

.clear_specsvoid

This method returns an undefined value.

Clear all loaded specifications (primarily for testing)



70
71
72
# File 'lib/omnizip/formats/format_spec_loader.rb', line 70

def clear_specs
  @all_specs = {}
end

.get(format_name) ⇒ FormatSpecification?

Get a loaded specification

Parameters:

  • format_name (String)

    The format name

Returns:



86
87
88
# File 'lib/omnizip/formats/format_spec_loader.rb', line 86

def get(format_name)
  all_specs[format_name]
end

.load(format_name, config_dir: default_config_dir) ⇒ FormatSpecification

Load a format specification from a YAML file

Parameters:

  • format_name (String)

    The name of the format (e.g., "rar5")

  • config_dir (String) (defaults to: default_config_dir)

    The directory containing format specs

Returns:

Raises:

  • (FormatError)

    If the specification file is not found or invalid



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/omnizip/formats/format_spec_loader.rb', line 35

def load(format_name, config_dir: default_config_dir)
  spec_file = File.join(config_dir, "#{format_name}_spec.yml")

  unless File.exist?(spec_file)
    raise FormatError,
          "Format specification not found: #{spec_file}"
  end

  yaml_content = File.read(spec_file)
  parsed_yaml = YAML.safe_load(
    yaml_content,
    permitted_classes: [Symbol],
    symbolize_names: true,
  )

  validate_spec(parsed_yaml, format_name)

  spec = FormatSpecification.new(parsed_yaml)
  register_spec(format_name, spec)
  spec
rescue Psych::SyntaxError => e
  raise FormatError,
        "Invalid YAML in #{spec_file}: #{e.message}"
end

.load_all(config_dir: default_config_dir) ⇒ Hash<String, FormatSpecification>

Load all format specifications from a directory

Parameters:

  • config_dir (String) (defaults to: default_config_dir)

    The directory containing format specs

Returns:



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/omnizip/formats/format_spec_loader.rb', line 94

def load_all(config_dir: default_config_dir)
  return all_specs unless Dir.exist?(config_dir)

  Dir.glob(File.join(config_dir, "*_spec.yml")).each do |spec_file|
    format_name = File.basename(spec_file, "_spec.yml")
    unless loaded?(format_name)
      load(format_name,
           config_dir: config_dir)
    end
  end

  all_specs
end

.loaded?(format_name) ⇒ Boolean

Check if a format specification is loaded

Parameters:

  • format_name (String)

    The format name

Returns:

  • (Boolean)

    True if loaded, false otherwise



78
79
80
# File 'lib/omnizip/formats/format_spec_loader.rb', line 78

def loaded?(format_name)
  all_specs.key?(format_name)
end