Class: Extruder::Filter Abstract
- Inherits:
-
Object
- Object
- Extruder::Filter
- Defined in:
- lib/extruder/filter.rb
Overview
This abstract class is used for building Filters that will be assigned to an Extruder. Their function is to transform one array of FileWrappers to another.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#files ⇒ Array[FileWrapper]
The files that this filter will operate on.
-
#output_name_generator ⇒ Proc
A block that returns the relative output filename for a particular input file.
-
#output_root ⇒ String
The expanded root path that this filter’s created files will eventually be written to.
Class Method Summary collapse
-
.processes_binary_files ⇒ void
Invoke this method in a subclass of Filter to declare that it expects to work with BINARY data, and that data that is not valid UTF-8 should be allowed.
Instance Method Summary collapse
-
#initialize(&block) ⇒ Filter
constructor
…more soon.
-
#outputs ⇒ Hash{FileWrapper => Array<FileWrapper>}
A hash of output files pointing at their associated input files.
-
#process ⇒ Array{FileWrapper}
For each FileWrapper file this filter will create, call generate_output (defined in subclasses of this) to fill the These files should be flagged as saveable so the FileSet that they wind up in will know they should ultimately be written to the output directory.
Constructor Details
#initialize(&block) ⇒ Filter
…more soon
46 47 48 49 50 51 52 53 |
# File 'lib/extruder/filter.rb', line 46 def initialize(&block) if self.respond_to?(:external_dependencies) require_dependencies! end block ||= proc { |input| input } @output_name_generator = block @files = [] end |
Instance Attribute Details
#files ⇒ Array[FileWrapper]
Returns the files that this filter will operate on.
15 16 17 |
# File 'lib/extruder/filter.rb', line 15 def files @files end |
#output_name_generator ⇒ Proc
Returns a block that returns the relative output filename for a particular input file. If the block accepts just one argument, it will be passed the input’s path. If it accepts two, it will also be passed the file contents.
25 26 27 |
# File 'lib/extruder/filter.rb', line 25 def output_name_generator @output_name_generator end |
#output_root ⇒ String
Returns the expanded root path that this filter’s created files will eventually be written to.
19 20 21 |
# File 'lib/extruder/filter.rb', line 19 def output_root @output_root end |
Class Method Details
.processes_binary_files ⇒ void
This method returns an undefined value.
Invoke this method in a subclass of Filter to declare that it expects to work with BINARY data, and that data that is not valid UTF-8 should be allowed.
35 36 37 |
# File 'lib/extruder/filter.rb', line 35 def self.processes_binary_files define_method(:encoding) { "BINARY" } end |
Instance Method Details
#outputs ⇒ Hash{FileWrapper => Array<FileWrapper>}
A hash of output files pointing at their associated input files. The output names are created by applying the #output_name_generator to each input file.
For exmaple, if you had the following input files:
javascripts/jquery.js
javascripts/sproutcore.js
stylesheets/sproutcore.css
And you had the following #output_name_generator:
filter.output_name_generator = proc do |filename|
# javascripts/jquery.js becomes:
# ["javascripts", "jquery", "js"]
directory, file, ext = file.split(/[\.\/]/)
"#{directory}.#{ext}"
end
You would end up with the following hash:
{
#<FileWrapper path="javascripts.js"> => [
#<FileWrapper path="javascripts/jquery.js">,
#<FileWrapper path="javascripts/sproutcore.js">
],
#<FileWrapper path="stylesheets.css"> => [
#<FileWrapper path="stylesheets/sproutcore.css">
]
}
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/extruder/filter.rb', line 93 def outputs hash = {} @files.each do |file| outputs = output_paths(file) output_wrappers(file).each do |output| hash[output] ||= [] hash[output] << file end end hash end |
#process ⇒ Array{FileWrapper}
For each Extruder::FileWrapper file this filter will create, call generate_output (defined in subclasses of this) to fill the These files should be flagged as saveable so the Extruder::FileSet that they wind up in will know they should ultimately be written to the output directory.
121 122 123 124 125 126 127 |
# File 'lib/extruder/filter.rb', line 121 def process outputs.map do |output, inputs| generate_output(inputs, output) output.save = true output end end |