Class: Rake::Pipeline::Web::Filters::ChainedFilter

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

Overview

The purpose of ChainedFilter is to enable filters to be applied to files based upon their file extensions.

Filters are applied repeatedly to files for each extension.

In this example, files with the extensions erb, coffee, and scss will be processed using the specified filters. If a file has multiple extensions, all of the filters will be applied.

For example, with the above filter specification, a file like application.js.coffee.erb will first apply the ErbFilter, then the CoffeeFilter, and then output application.js.

This filter is largely designed for use with the register helper, which will transparently add a ChainedFilter before each input block with the registered extensions.

Examples:


filter ChainedFilter, :types => {
  :erb => ErbFilter,
  :coffee => CoffeeFilter,
  :scss => ScssFilter
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ChainedFilter.

Parameters:

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

Options Hash (options):

  • :types (Hash)

    A hash of file extensions and their associated filters. See the class description for more information.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rake-pipeline-web-filters/chained_filter.rb', line 63

def initialize(options={}, &block)
  @filters = options[:types]

  keys = @filters.keys
  pattern = keys.map { |key| "\\.#{key}" }.join("|")
  @pattern = Regexp.new("(#{pattern})*$", "i")

  block ||= proc do |input|
    input.sub(@pattern, '')
  end

  super(&block)
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



56
57
58
# File 'lib/rake-pipeline-web-filters/chained_filter.rb', line 56

def filters
  @filters
end

Instance Method Details

#generate_output(inputs, output) ⇒ Object

Implement generate_output



80
81
82
83
84
# File 'lib/rake-pipeline-web-filters/chained_filter.rb', line 80

def generate_output(inputs, output)
  inputs.each do |input|
    output.write process_filters(input)
  end
end

#process_filters(input) ⇒ Object

Process an input file by applying the filter for each extension in the file.



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rake-pipeline-web-filters/chained_filter.rb', line 90

def process_filters(input)
  keys = input.path.match(@pattern)[0].scan(/(?<=\.)\w+/)

  filters = keys.reverse_each.map do |key|
    @filters[key.to_sym]
  end

  filters.each do |filter|
    input = process_with_filter(input, filter)
  end

  input.read
end

#process_with_filter(input, filter_class) ⇒ Object

Process an individual file with a filter.



107
108
109
110
111
112
113
114
# File 'lib/rake-pipeline-web-filters/chained_filter.rb', line 107

def process_with_filter(input, filter_class)
  filter = filter_class.new

  output = MemoryFileWrapper.new("/output", input.path, "UTF-8")
  output.create { filter.generate_output([input], output) }

  output
end