Class: Rake::Pipeline::Web::Filters::CacheBusterFilter

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

Overview

A filter that inserts a cache-busting key into the outputted file name.

Examples:

Rake::Pipeline.build do
  input "app/assets"
  output "public"

  filter Rake::Pipeline::Web::Filters::CacheBusterFilter
end

Constant Summary collapse

DEFAULT_KEY_GENERATOR =
proc { |input|
  require 'digest/md5'
  Digest::MD5.new << input.path << input.read
}

Instance Method Summary collapse

Constructor Details

#initialize(&key_generator) ⇒ CacheBusterFilter


24
25
26
27
28
29
30
31
32
33
# File 'lib/rake-pipeline-web-filters/cache_buster_filter.rb', line 24

def initialize(&key_generator)
  key_generator ||= DEFAULT_KEY_GENERATOR
  output_name_generator = proc { |path, file|
    parts = path.split('.')
    index_to_modify = parts.length > 1 ? -2 : -1
    parts[index_to_modify] << "-#{key_generator.call(file)}"
    parts.join('.')
  }
  super(&output_name_generator)
end

Instance Method Details

#generate_output(inputs, output) ⇒ Object


35
36
37
38
39
# File 'lib/rake-pipeline-web-filters/cache_buster_filter.rb', line 35

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