Class: TableStructure::CSV::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/table_structure/csv/writer.rb

Constant Summary collapse

BOM =
"\uFEFF"

Instance Method Summary collapse

Constructor Details

#initialize(schema, bom: false, csv_options: {}, header: { context: nil, step: nil }) ⇒ Writer

Returns a new instance of Writer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/table_structure/csv/writer.rb', line 8

def initialize(
  schema,
  bom: false,
  csv_options: {},
  header: { context: nil, step: nil }
)
  require 'csv'

  @options = {
    bom: bom,
    csv_options: csv_options
  }
  inner_options = {
    header: header
  }

  @writer = ::TableStructure::Writer.new(schema, **inner_options)
end

Instance Method Details

#write(items, to:, bom: @options[:bom], csv_options: @options[:csv_options], &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/table_structure/csv/writer.rb', line 27

def write(
  items,
  to:,
  bom: @options[:bom],
  csv_options: @options[:csv_options],
  &block
)
  to << BOM if bom

  csv = ::CSV.new(to, **csv_options)
  @writer.write(items, to: csv, &block)
end