Class: Alf::Renderer::CSV

Inherits:
Alf::Renderer show all
Includes:
Support::CSVUtils
Defined in:
lib/alf/renderer/csv.rb

Overview

Implements Alf::Renderer contract for outputting CSV files.

Constant Summary

Constants included from Support::CSVUtils

Support::CSVUtils::DEFAULT_OPTIONS

Constants inherited from Alf::Renderer

DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Alf::Renderer

#input, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Alf::Renderer

by_mime_type, #execute, #initialize, register, renderer

Methods included from Support::Registry

#listen, #listeners, #register, #registered

Constructor Details

This class inherits a constructor from Alf::Renderer

Class Method Details

.mime_typeObject


9
10
11
# File 'lib/alf/renderer/csv.rb', line 9

def self.mime_type
  "text/csv"
end

Instance Method Details

#each(&bl) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/alf/renderer/csv.rb', line 13

def each(&bl)
  return to_enum unless block_given?
  with_csv(ProcIO.new(bl), options.merge(row_sep: "\n")) do |csv|
    header = nil
    each_tuple do |tuple|
      unless header
        header = tuple.keys
        csv << header.map(&:to_s)
      end
      csv << header.map{|k| tuple[k]}
    end
  end
end