Class: TableStructure::CSV::Writer

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

Constant Summary collapse

DEFAULT_OPTIONS =
{
  bom: false,
  csv_options: {}
}.freeze
INNER_WRITER_DEFAULT_OPTIONS =
::TableStructure::Writer::DEFAULT_OPTIONS
INNER_WRITER_IGNORED_OPTION_KEYS =
i[
  result_type
  method
].freeze
BOM =
"\uFEFF"

Instance Method Summary collapse

Constructor Details

#initialize(schema, **options) ⇒ Writer

Returns a new instance of Writer.



19
20
21
22
23
24
# File 'lib/table_structure/csv/writer.rb', line 19

def initialize(schema, **options)
  require 'csv'
  @options = DEFAULT_OPTIONS.merge(select_csv_writer_options(options))
  inner_writer_options = select_inner_writer_options(options)
  @writer = ::TableStructure::Writer.new(schema, **inner_writer_options)
end

Instance Method Details

#write(items, to:, **options, &block) ⇒ Object



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

def write(items, to:, **options, &block)
  csv_writer_options = @options.merge(select_csv_writer_options(options))
  inner_writer_options = select_inner_writer_options(options)
  if csv_writer_options[:bom]
    to.send(INNER_WRITER_DEFAULT_OPTIONS[:method], BOM)
  end
  csv = ::CSV.new(to, **csv_writer_options[:csv_options])
  @writer.write(items, to: csv, **inner_writer_options, &block)
end