Class: Extruder::Filter Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/extruder/filter.rb

Overview

This class is abstract.

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

ConcatFilter, SassFilter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Filter

…more soon

Parameters:



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

#filesArray[FileWrapper]

Returns the files that this filter will operate on.

Returns:

  • (Array[FileWrapper])

    the files that this filter will operate on.



15
16
17
# File 'lib/extruder/filter.rb', line 15

def files
  @files
end

#output_name_generatorProc

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.

Returns:

  • (Proc)

    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_rootString

Returns the expanded root path that this filter’s created files will eventually be written to.

Returns:

  • (String)

    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_filesvoid

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

#outputsHash{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">
  ]
}

Returns:



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

#processArray{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.

Returns:

  • (Array{FileWrapper})

    An array of FileWrapped ouputs for this filter.



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