Class: Rake::Pipeline::Web::Filters::GzipFilter

Inherits:
Filter
  • Object
show all
Includes:
FilterWithDependencies
Defined in:
lib/rake-pipeline-web-filters/gzip_filter.rb

Overview

A filter that gzips input files using zlib

Examples:

Rake::Pipeline.build do
  input "app/assets", "**/*.js"
  output "public"

  # Compress each JS file under the app/assets
  # directory.
  filter Rake::Pipeline::Web::Filters::GzipFilter
end

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ GzipFilter

Returns a new instance of GzipFilter.

Parameters:

  • block (Proc)

    a block to use as the Filter’s #output_name_generator.



25
26
27
28
# File 'lib/rake-pipeline-web-filters/gzip_filter.rb', line 25

def initialize(&block)
  block ||= proc { |input| input.sub(/\.(.*)$/, '.\1.gz') }
  super(&block)
end

Instance Method Details

#generate_output(inputs, output) ⇒ Object

Implement the #generate_output method required by the Filter API. Compresses each input file with Zlib.GzipWriter.

Parameters:

  • inputs (Array<FileWrapper>)

    an Array of FileWrapper objects representing the inputs to this filter.

  • output (FileWrapper)

    a single FileWrapper object representing the output.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rake-pipeline-web-filters/gzip_filter.rb', line 39

def generate_output(inputs, output)
  inputs.each do |input|
    # gzip input file to stream
    fakefile = StringIO.new
    gz = Zlib::GzipWriter.new(fakefile)
    gz.write(input.read)
    gz.close

    # send gzipped contents to output
    output.write fakefile.string
  end
end