class Tilt::CSVTemplate

CSV Template implementation. See: 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)

Public Class Methods

engine() click to toggle source
# File lib/tilt/csv.rb, line 39
def self.engine
  if RUBY_VERSION >= '1.9.0' && defined? ::CSV
    ::CSV
  elsif defined? ::FasterCSV
    ::FasterCSV 
  end
end

Public Instance Methods

precompiled(locals) click to toggle source
Calls superclass method
# File lib/tilt/csv.rb, line 59
def precompiled(locals)
  source, offset = super
  [source, offset + 1]
end
precompiled_template(locals) click to toggle source
# File lib/tilt/csv.rb, line 55
def precompiled_template(locals)
  @code
end
prepare() click to toggle source
# File lib/tilt/csv.rb, line 47
def prepare
  @code =<<-RUBY
    #{self.class.engine}.generate do |csv|
      #{data}
    end
  RUBY
end