Class: Tilt::CSVTemplate

Inherits:
Template show all
Defined in:
lib/vendor/tilt-1.4.1/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

#allows_script?, #basename, #default_encoding, #eval_file, #initialize, #name, #read_template_file, #render

Constructor Details

This class inherits a constructor from Tilt::Template

Class Method Details

.engineObject



37
38
39
40
41
42
43
# File 'lib/vendor/tilt-1.4.1/lib/tilt/csv.rb', line 37

def self.engine
  if RUBY_VERSION >= '1.9.0' && defined? ::CSV
    ::CSV
  elsif defined? ::FasterCSV
    ::FasterCSV 
  end
end

.engine_initialized?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/vendor/tilt-1.4.1/lib/tilt/csv.rb', line 33

def self.engine_initialized?
  engine
end

Instance Method Details

#initialize_engineObject



45
46
47
48
49
50
51
# File 'lib/vendor/tilt-1.4.1/lib/tilt/csv.rb', line 45

def initialize_engine
  if RUBY_VERSION >= '1.9.0'
    require_template_library 'csv'
  else
    require_template_library 'fastercsv'
  end
end

#precompiled(locals) ⇒ Object



65
66
67
68
# File 'lib/vendor/tilt-1.4.1/lib/tilt/csv.rb', line 65

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

#precompiled_template(locals) ⇒ Object



61
62
63
# File 'lib/vendor/tilt-1.4.1/lib/tilt/csv.rb', line 61

def precompiled_template(locals)
  @code
end

#prepareObject



53
54
55
56
57
58
59
# File 'lib/vendor/tilt-1.4.1/lib/tilt/csv.rb', line 53

def prepare
  @code =<<-RUBY
    #{self.class.engine}.generate do |csv|
      #{data}
    end
  RUBY
end