Class: Railsmdb::Extractor

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

Overview

A utility class for extracting a specific file from a given archive. Currently only supports .tgz, .tar.gz, and .zip archives.

Direct Known Subclasses

TarGzipExtractor, ZipExtractor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(archive_path) ⇒ Extractor

Instantiates a new extractor with the given archive path.

Parameters:

  • archive_path (String)

    the path to the archive.



31
32
33
# File 'lib/railsmdb/extractor.rb', line 31

def initialize(archive_path)
  @archive_path = archive_path
end

Instance Attribute Details

#archive_pathObject (readonly)

The path to the archive.



26
27
28
# File 'lib/railsmdb/extractor.rb', line 26

def archive_path
  @archive_path
end

Class Method Details

.for(archive_path) ⇒ ZipExtractor | TarGzipExtractor

Returns the appropriate extractor class for the given archive path.

Parameters:

  • archive_path (String)

    the path to the archive file

Returns:



17
18
19
20
21
22
23
# File 'lib/railsmdb/extractor.rb', line 17

def self.for(archive_path)
  case archive_path
  when /\.(tgz|tar\.gz)$/ then TarGzipExtractor.new(archive_path)
  when /\.zip$/ then ZipExtractor.new(archive_path)
  else raise ArgumentError, "don't know how to extract #{archive_path}"
  end
end

Instance Method Details

#extract(pattern) {|String, String| ... } ⇒ String | nil

Extract the first file that matches the given pattern from the archive.

Parameters:

  • pattern (Regexp)

    the pattern to use for finding the file

Yields:

  • (String, String)

    report the name and contents of the first matching file. It will never yield more than once.

Returns:

  • (String | nil)

    returns the name of the matching file, or nil if no file matched.

Raises:

  • (NotImplementedError)

    subclasses must override this method.



47
48
49
# File 'lib/railsmdb/extractor.rb', line 47

def extract(pattern)
  raise NotImplementedError
end