Sha256: 92d41174c3ba0df2d61f3422e9578c61cec6c8e6bac9ff55a5c89144c3f34cf0

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 KB

Contents

require 'tilt/template'

if RUBY_VERSION >= '1.9.0'
  require 'csv'
else
  require 'fastercsv'
end

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
      if RUBY_VERSION >= '1.9.0' && defined? ::CSV
        ::CSV
      elsif defined? ::FasterCSV
        ::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

Version data entries

8 entries across 8 versions & 4 rubygems

Version Path
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/tilt-2.0.1/lib/tilt/csv.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/tilt-2.0.1/lib/tilt/csv.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/tilt-2.0.1/lib/tilt/csv.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/tilt-2.0.1/lib/tilt/csv.rb
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/tilt-2.0.1/lib/tilt/csv.rb
tilt-2.0.1 lib/tilt/csv.rb
tilt-2.0.0 lib/tilt/csv.rb
tilt-2.0.0.beta1 lib/tilt/csv.rb