Class: TableStructure::CSV::Writer

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

Constant Summary collapse

DEFAULT_OPTIONS =
{
  bom: false,
  header_omitted: false,
  header_context: nil
}.freeze
FIXED_OPTIONS =
{
  result_type: :array,
  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
# File 'lib/table_structure/csv/writer.rb', line 19

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

Instance Method Details

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



25
26
27
28
29
# File 'lib/table_structure/csv/writer.rb', line 25

def write(items, to:, **options, &block)
  options = @options.merge(options).merge(FIXED_OPTIONS)
  to.send(options[:method], BOM) if options[:bom]
  @writer.write(items, to: ::CSV.new(to), **options, &block)
end