Class: Tilt::CSVTemplate

Inherits:
Template show all
Defined in:
lib/tilt/csv.rb

Overview

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)

Instance Attribute Summary

Attributes inherited from Template

#data, #file, #line, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Template

#basename, default_mime_type, default_mime_type=, #eval_file, #initialize, metadata, #metadata, #name, #render

Constructor Details

This class inherits a constructor from Tilt::Template

Class Method Details

.engineObject



39
40
41
42
43
44
45
# 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

Instance Method Details

#precompiled(locals) ⇒ Object



59
60
61
62
# File 'lib/tilt/csv.rb', line 59

def precompiled(locals)
  source, offset = super
  [source, offset + 1]
end

#precompiled_template(locals) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/tilt/csv.rb', line 51

def precompiled_template(locals)
  <<-RUBY
    #{@outvar} = #{self.class.engine}.generate(#{options}) do |csv|
      #{data}
    end
  RUBY
end

#prepareObject



47
48
49
# File 'lib/tilt/csv.rb', line 47

def prepare
  @outvar = options.delete(:outvar) || '_csvout'
end