Sha256: 355351fb2a16e35e7ce38c934fbbc752b2b31fe0b68927f592d347d0382da4dc

Contents?: true

Size: 1.27 KB

Versions: 174

Compression:

Stored size: 1.27 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
      @outvar = options.delete(:outvar) || '_csvout'
    end

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

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

  end
end

Version data entries

174 entries across 137 versions & 24 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/tilt-2.0.10/lib/tilt/csv.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/tilt-2.0.10/lib/tilt/csv.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/tilt-2.0.10/lib/tilt/csv.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/tilt-2.0.10/lib/tilt/csv.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/tilt-2.0.10/lib/tilt/csv.rb
brakeman-5.2.3 bundle/ruby/2.7.0/gems/tilt-2.0.10/lib/tilt/csv.rb
brakeman-5.2.2 bundle/ruby/2.7.0/gems/tilt-2.0.10/lib/tilt/csv.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/tilt-2.0.10/lib/tilt/csv.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.0.0/gems/tilt-2.0.10/lib/tilt/csv.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/tilt-2.0.10/lib/tilt/csv.rb
brakeman-5.2.1 bundle/ruby/2.7.0/gems/tilt-2.0.10/lib/tilt/csv.rb
brakeman-5.2.0 bundle/ruby/2.7.0/gems/tilt-2.0.10/lib/tilt/csv.rb
brakeman-5.1.2 bundle/ruby/2.7.0/gems/tilt-2.0.10/lib/tilt/csv.rb
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/tilt-2.0.9/lib/tilt/csv.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/tilt-2.0.9/lib/tilt/csv.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/tilt-2.0.9/lib/tilt/csv.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/tilt-2.0.9/lib/tilt/csv.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/tilt-2.0.9/lib/tilt/csv.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/tilt-2.0.9/lib/tilt/csv.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/tilt-2.0.9/lib/tilt/csv.rb