Class: Rake::Pipeline::Web::Filters::YUICssFilter

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

Overview

A filter that compresses CSS input files using the YUI CSS compressor.

Requires yui-compressor

Examples:

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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ YUICssFilter

Returns a new instance of YUICssFilter

Parameters:

  • options (Hash) (defaults to: {})

    options to pass to the YUI CSS compressor.

  • block (Proc)

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


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rake-pipeline-web-filters/yui_css_filter.rb', line 30

def initialize(options={}, &block)
  block ||= proc { |input| 
    if input =~ %r{min.css$}
      input
    else
      input.sub /\.css$/, '.min.css'
    end
  }

  super(&block)
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns a hash of options to pass to the YUI compressor when compressing.

Returns:

  • (Hash)

    a hash of options to pass to the YUI compressor when compressing.


24
25
26
# File 'lib/rake-pipeline-web-filters/yui_css_filter.rb', line 24

def options
  @options
end

Instance Method Details

#generate_output(inputs, output) ⇒ Object

Implement the #generate_output method required by the Filter API. Compresses each input file with the YUI CSS compressor.

Parameters:

  • inputs (Array<FileWrapper>)

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

  • output (FileWrapper)

    a single FileWrapper object representing the output.


52
53
54
55
56
57
58
59
60
61
# File 'lib/rake-pipeline-web-filters/yui_css_filter.rb', line 52

def generate_output(inputs, output)
  compressor = YUI::CssCompressor.new(options)
  inputs.each do |input|
    if input.path !~ /min\.css/
      output.write compressor.compress(input.read)
    else
      output.write input.read
    end
  end
end