class Rapper::Compressors::Compressor

Base class for a compression handler.

Attributes

extensions[RW]

Public Class Methods

compress( file_path, opts={} ) click to toggle source

Compress a file. Raises UnknownFileExtension if it doesn’t know how to compress a file with the given file’s file extension.

@param [String] file_path Path to the file to compress.

@param [Hash] opts Options to be passed to the compressor (optional).

# File lib/rapper/compressors.rb, line 34
def compress( file_path, opts={} )
  unless compressor = @extensions[File.extname( file_path )]
    raise Rapper::Errors::UnknownFileExtension,
      "Rapper doesn't know how to compress #{file_path}"
  end
  
  compressor.do_compress( file_path, opts )
end

Protected Class Methods

do_compress( file_path ) click to toggle source

Compress a file.

@param [String] file_path Path to the file to compress.

# File lib/rapper/compressors.rb, line 58
def do_compress( file_path )
  raise NotImplementedError
end
read_file( file_path ) click to toggle source

@param [String] file_path Path to a file.

@return [String] The contents of the file.

# File lib/rapper/compressors.rb, line 65
def read_file( file_path )
  File.new( file_path, 'r' ).read
end
register( extension ) click to toggle source

Register ‘self` as a file compressor.

@param [String] extension The file extension ‘self` handles.

# File lib/rapper/compressors.rb, line 50
def register( extension )
  superclass.extensions ||= {}
  superclass.extensions[extension] = self
end
writable_file( file_path ) click to toggle source

@param [String] file_path Path to the desired file.

@return [File] Writable file instance with 0644 permissions.

# File lib/rapper/compressors.rb, line 72
def writable_file( file_path )
  File.new( file_path, 'w', 0644 )
end