Class: Excavate::Extractors::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/excavate/extractors/extractor.rb

Overview

Base class for all archive extractors.

Subclasses register the magic-byte types they handle by calling handles :type, ... at class-body load time. The base class maintains the registry; new formats are added by creating a new subclass file with a handles declaration, with no edits to this file or to any branching logic.

Constant Summary collapse

@@registry =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(archive) ⇒ Extractor

Returns a new instance of Extractor.



50
51
52
# File 'lib/excavate/extractors/extractor.rb', line 50

def initialize(archive)
  @archive = archive
end

Class Method Details

.eager_load_subclasses!Object

Force-load every autoloaded constant under Extractors so that each subclass's handles declaration runs. Idempotent.



45
46
47
# File 'lib/excavate/extractors/extractor.rb', line 45

def eager_load_subclasses!
  Extractors.constants.each { |name| Extractors.const_get(name) }
end

.for_magic_type(type) ⇒ Object

Resolve the extractor class for a magic-byte type, or nil if no extractor is registered. Triggers eager load of all extractor subclasses on first miss so every handles declaration has had a chance to fire.



26
27
28
29
30
31
# File 'lib/excavate/extractors/extractor.rb', line 26

def for_magic_type(type)
  return @@registry[type] if @@registry.key?(type)

  eager_load_subclasses!
  @@registry[type]
end

.handles(*magic_types) ⇒ Object

Declare which magic-byte types this extractor handles. Called at class-body load time in each subclass.



18
19
20
# File 'lib/excavate/extractors/extractor.rb', line 18

def handles(*magic_types)
  magic_types.each { |type| @@registry[type] = self }
end

.registered_typesObject



39
40
41
# File 'lib/excavate/extractors/extractor.rb', line 39

def registered_types
  @@registry.keys
end

.registryObject

The full type → class registry. Exposed for introspection (specs, debugging). Not for mutation.



35
36
37
# File 'lib/excavate/extractors/extractor.rb', line 35

def registry
  @@registry
end

Instance Method Details

#extract(_target) ⇒ Object

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/excavate/extractors/extractor.rb', line 54

def extract(_target)
  raise NotImplementedError, "you must implement #extract"
end