Class: Rake::Pipeline::Web::Filters::ChainedFilter
- Inherits:
-
Filter
- Object
- Filter
- Rake::Pipeline::Web::Filters::ChainedFilter
- 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.
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
Instance Method Summary collapse
-
#generate_output(inputs, output) ⇒ Object
Implement
generate_output
. -
#initialize(options = {}, &block) ⇒ ChainedFilter
constructor
A new instance of ChainedFilter.
-
#process_filters(input) ⇒ Object
Process an input file by applying the filter for each extension in the file.
-
#process_with_filter(input, filter_class) ⇒ Object
Process an individual file with a filter.
Constructor Details
#initialize(options = {}, &block) ⇒ ChainedFilter
Returns a new instance of ChainedFilter.
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(={}, &block) @filters = [: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
#filters ⇒ Object (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 |