Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

CSV templates #153

Merged
merged 4 commits into from

3 participants

@alexgb

I think the use case is somewhat common and similar to the need for Builder templates. Being that CSV is standard library now, keeping the template implementation for CSV in Tilt seems sensible.

@judofyr
Collaborator

engine_initialized? should return true/false, but engine raises an error if the engine is uninitialized. You (unfortunately) need something like this:

if RUBY_VERSION >= '1.9.0'
  defined? ::CSV
else
  defined? ::FasterCSV
end

(I'm not sure if this ought to be merged or not though; I can't guarantee that it will be merged even if you fix this issue.)

@rtomayko
Owner

Can you update the list of engines in README and add some docs to the Tilt::CSVTemplate class? The docs on Tilt::CSVTemplate should have a quick example template or something. Just enough to give a feel for what a .csv file should look like.

@judofyr judofyr merged commit 8bcd432 into rtomayko:master
@judofyr
Collaborator

Thanks for the contribution, this looks really good :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 13, 2012
  1. @alexgb
Commits on Aug 2, 2012
  1. @alexgb
Commits on Aug 5, 2012
  1. @alexgb

    Fix csv engine selection.

    alexgb authored
Commits on Aug 6, 2012
  1. @alexgb
This page is out of date. Refresh to see the latest.
View
1  README.md
@@ -46,6 +46,7 @@ Support for these template engines is included with the package:
Creole (Wiki markup) .wiki, .creole creole
WikiCloth (Wiki markup) .wiki, .mediawiki, .mw wikicloth
Yajl .yajl yajl-ruby
+ CSV .csv none (Ruby >= 1.9), fastercsv (Ruby < 1.9)
These template engines ship with their own Tilt integration:
View
3  lib/tilt.rb
@@ -150,6 +150,9 @@ def clear
register ScssTemplate, 'scss'
register LessTemplate, 'less'
+ require 'tilt/csv'
+ register CSVTemplate, 'csv'
+
require 'tilt/coffee'
register CoffeeScriptTemplate, 'coffee'
View
71 lib/tilt/csv.rb
@@ -0,0 +1,71 @@
+require 'tilt/template'
+
+module Tilt
+
+ # CSV Template implementation. See:
+ # http://ruby-doc.org/stdlib/libdoc/csv/rdoc/CSV.html
+ #
+ # == Example
+ #
+ # # Example of csv template
+ # tpl = <<-EOS
+ # # header
+ # csv << ['NAME', 'ID']
+ #
+ # # data rows
+ # @people.each do |person|
+ # csv << [person[:name], person[:id]]
+ # end
+ # EOS
+ #
+ # @people = [
+ # {:name => "Joshua Peek", :id => 1},
+ # {:name => "Ryan Tomayko", :id => 2},
+ # {:name => "Simone Carletti", :id => 3}
+ # ]
+ #
+ # template = Tilt::CSVTemplate.new { tpl }
+ # template.render(self)
+ #
+ class CSVTemplate < Template
+ self.default_mime_type = 'text/csv'
+
+ def self.engine_initialized?
+ engine
+ end
+
+ def self.engine
+ if RUBY_VERSION >= '1.9.0' && defined? ::CSV
+ ::CSV
+ elsif defined? ::FasterCSV
+ ::FasterCSV
+ end
+ end
+
+ def initialize_engine
+ if RUBY_VERSION >= '1.9.0'
+ require_template_library 'csv'
+ else
+ require_template_library 'fastercsv'
+ end
+ end
+
+ def prepare
+ @code =<<-RUBY
+ #{self.class.engine}.generate do |csv|
+ #{data}
+ end
+ RUBY
+ end
+
+ def precompiled_template(locals)
+ @code
+ end
+
+ def precompiled(locals)
+ source, offset = super
+ [source, offset + 1]
+ end
+
+ end
+end
View
73 test/tilt_csv_test.rb
@@ -0,0 +1,73 @@
+require 'contest'
+require 'tilt'
+
+begin
+ if RUBY_VERSION >= '1.9.0'
+ require 'csv'
+ else
+ require 'fastercsv'
+ end
+
+ class CSVTemplateTest < Test::Unit::TestCase
+
+ test "is registered for '.csv' files" do
+ assert_equal Tilt::CSVTemplate, Tilt['test.csv']
+ end
+
+ test "registered for '.csv' files" do
+ assert Tilt.mappings['csv'].include?(Tilt::CSVTemplate)
+ end
+
+ test "compiles and evaluates the template on #render" do
+ template = Tilt::CSVTemplate.new { "csv << ['hello', 'world']" }
+ assert_equal "hello,world\n", template.render
+ end
+
+ test "can be rendered more than once" do
+ template = Tilt::CSVTemplate.new { "csv << [1,2,3]" }
+ 3.times { assert_equal "1,2,3\n", template.render }
+ end
+
+ test "can pass locals" do
+ template = Tilt::CSVTemplate.new { 'csv << [1, name]' }
+ assert_equal "1,Joe\n", template.render(Object.new, :name => 'Joe')
+ end
+
+ test "evaluating in an object scope" do
+ template = Tilt::CSVTemplate.new { 'csv << [1, @name]' }
+ scope = Object.new
+ scope.instance_variable_set :@name, 'Joe'
+ assert_equal "1,Joe\n", template.render(scope)
+ end
+
+ test "backtrace file and line reporting" do
+ data = File.read(__FILE__).split("\n__END__\n").last
+ template = Tilt::CSVTemplate.new('test.csv') { data }
+ begin
+ template.render
+ fail 'should have raised an exception'
+ rescue => boom
+ assert_kind_of NameError, boom
+ line = boom.backtrace.grep(/^test\.csv:/).first
+ assert line, "Backtrace didn't contain test.csv"
+ file, line, meth = line.split(":")
+ assert_equal '4', line
+ end
+ end
+
+ end
+
+rescue LoadError => boom
+ warn "Tilt::CSVTemplate (disabled) please install 'fastercsv' if using ruby 1.8.x"
+end
+
+
+__END__
+# header
+csv << ['Type', 'Age']
+
+raise NameError
+
+# rows
+csv << ['Frog', 2]
+csv << ['Cat', 5]
Something went wrong with that request. Please try again.