Module: Rgviz::CsvRenderer

Defined in:
lib/rgviz/csv_renderer.rb

Class Method Summary collapse

Class Method Details

.format(value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rgviz/csv_renderer.rb', line 23

def self.format(value)
  value = value.to_s.gsub('"', '""')

  return "\"#{value}\"" if value.start_with?(' ') || value.end_with?(' ')

  len = value.length
  value.chars.each do |c|
    return "\"#{value}\"" if c == ',' || c == "\n" || c == "\r" || c == "\t"
  end

  value
end

.render(table) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rgviz/csv_renderer.rb', line 3

def self.render(table)
  string = ''
  table.cols.each_with_index do |col, i|
    string << ',' if i > 0
    string << CsvRenderer.format(col.label)
  end

  length = table.rows.length
  string << "\n" if length > 0

  table.rows.each_with_index do |row, i|
    row.c.each_with_index do |col, j|
      string << ',' if j > 0
      string << CsvRenderer.format((col.f || col.v).to_s)
    end
    string << "\n" if i != length - 1
  end
  string
end