Class: Rake::Pipeline::GsubFilter

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

Overview

A built in filter that applies String#gsub behavior.

Examples:

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

  # replace javascript comments
  filter(Rake::Pipeline::GsubFilter, /\//\w+$/, '')
end

Instance Attribute Summary

Attributes inherited from Filter

#file_wrapper_class, #input_files, #last_manifest, #manifest, #output_name_generator, #output_root, #pipeline, #rake_application, #rake_tasks

Instance Method Summary collapse

Methods inherited from Filter

#additional_dependencies, #generate_rake_tasks, #output_files, #outputs, processes_binary_files

Constructor Details

#initialize(*args, &block) ⇒ GsubFilter

Arguments mimic String#gsub with one notable exception. String#gsub accepts a block where $1, $2, and friends are accessible. Due to Ruby's scoping rules of these variables they are not accssible inside the block itself. Instead they are passed in as additional arguments. Here's an example:

Examples:

Rake::Pipeline::GsubFilter.new /(\w+)\s(\w+)/ do |entire_match, capture1, capture2|
  # process the match
end

See Also:

  • String#gsub

28
29
30
31
# File 'lib/rake-pipeline/filters/gsub_filter.rb', line 28

def initialize(*args, &block)
  @args, @block = args, block
  super() { |input| input }
end

Instance Method Details

#generate_output(inputs, output) ⇒ Object

Implement the #generate_output method required by the Filter API. In this case, simply loop through the inputs and write String#gsub content to the output.


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rake-pipeline/filters/gsub_filter.rb', line 42

def generate_output(inputs, output)
  inputs.each do |input|
    if @block
      content = input.read.gsub(*@args) do |match|
        @block.call match, *$~.captures
      end
      output.write content
    else
      output.write input.read.gsub(*@args)
    end
  end
end