Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERB Filter #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rake-pipeline-web-filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ module Filters
require "rake-pipeline-web-filters/chained_filter"
require "rake-pipeline-web-filters/less_filter"
require "rake-pipeline-web-filters/handlebars_filter"
require "rake-pipeline-web-filters/erb_filter"
require "rake-pipeline-web-filters/helpers"
49 changes: 49 additions & 0 deletions lib/rake-pipeline-web-filters/erb_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'rake-pipeline-web-filters/filter_with_dependencies'

module Rake::Pipeline::Web::Filters
# Evalute each file as an ERB template
#
# @example
# !!!ruby
# Rake::Pipeline.build do
# input "app/assets", "**/*.js.erb"
# output "public"
#
# # Compile each JS file under the app/assets
# # directory as an ERB template
# filter Rake::Pipeline::Web::Filters::ErbFilter
# end
class ErbFilter < Rake::Pipeline::Filter
include Rake::Pipeline::Web::Filters::FilterWithDependencies

# @param [Binding] binding to evaluate the template in
# @param [Proc] block a block to use as the Filter's
# {#output_name_generator}.
def initialize(binding = binding, &block)
@binding = binding
block ||= proc { |input| input.gsub(/\.erb$/, '') }
super(&block)
end

# Implement the {#generate_output} method required by
# the {Filter} API. Parses each file as an ERB template
# and evaluates it against the {binding}
#
# @param [Array<FileWrapper>] inputs an Array of
# {FileWrapper} objects representing the inputs to
# this filter.
# @param [FileWrapper] output a single {FileWrapper}
# object representing the output.
def generate_output(inputs, output)
inputs.each do |input|
output.write ERB.new(input.read).result(@binding)
end
end

private

def external_dependencies
[ 'erb' ]
end
end
end
6 changes: 6 additions & 0 deletions lib/rake-pipeline-web-filters/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def less(*args, &block)
def handlebars(*args, &block)
filter(Rake::Pipeline::Web::Filters::HandlebarsFilter, *args, &block)
end
#
# Add a new {ErbFilter} to the pipeline.
# @see ErbFilter#initialize
def erb(*args, &block)
filter(Rake::Pipeline::Web::Filters::ErbFilter, *args, &block)
end
end

module ProjectHelpers
Expand Down
47 changes: 47 additions & 0 deletions spec/erb_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
describe "ErbFilter" do
ErbFilter ||= Rake::Pipeline::Web::Filters::ErbFilter
MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper
User = Struct.new(:name)

let(:input) { "<%= user.name %>" }
let(:user) { User.new "Adam" }

def input_file(name, content)
MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content)
end

def output_file(name)
MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
end

def setup_filter(filter)
filter.file_wrapper_class = MemoryFileWrapper
filter.input_files = [input_file("test.html.erb", input)]
filter.output_root = "/path/to/output"
filter.rake_application = Rake::Application.new
filter
end

it "generates output" do
filter = setup_filter ErbFilter.new(binding)
filter.output_files.should == [output_file("test.html")]
tasks = filter.generate_rake_tasks
tasks.each(&:invoke)

file = MemoryFileWrapper.files["/path/to/output/test.html"]
file.body.should == user.name
file.encoding.should == "UTF-8"
end

describe "naming output files" do
it "strips .erb extensions by default" do
filter = setup_filter ErbFilter.new
filter.output_files.first.path.should == "test.html"
end

it "accepts a block to customize output file names" do
filter = setup_filter(ErbFilter.new { |input| "octopus" })
filter.output_files.first.path.should == "octopus"
end
end
end
7 changes: 7 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ def filter
filter.should be_kind_of(Rake::Pipeline::Web::Filters::HandlebarsFilter)
end
end

describe "#erb" do
it "creates ErbFilter" do
dsl.erb
filter.should be_kind_of(Rake::Pipeline::Web::Filters::ErbFilter)
end
end
end

describe "ProjectHelpers" do
Expand Down