Class: TableStructure::CSV::Writer
- Inherits:
-
Object
- Object
- TableStructure::CSV::Writer
- Defined in:
- lib/table_structure/csv/writer.rb
Constant Summary collapse
- BOM =
"\uFEFF"
Instance Method Summary collapse
-
#initialize(schema, bom: false, csv_options: {}, header: { context: nil }, **deprecated_options) ⇒ Writer
constructor
A new instance of Writer.
- #write(items, to:, bom: @options[:bom], csv_options: @options[:csv_options], **deprecated_options, &block) ⇒ Object
Constructor Details
#initialize(schema, bom: false, csv_options: {}, header: { context: nil }, **deprecated_options) ⇒ 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 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/table_structure/csv/writer.rb', line 8 def initialize( schema, bom: false, csv_options: {}, header: { context: nil }, ** ) if .key?(:header_omitted) header_omitted = [:header_omitted] warn "[TableStructure] `header_omitted: #{!!header_omitted}` option has been deprecated. Use `header: #{!header_omitted}` option instead." header = !header_omitted end if .key?(:header_context) header_context = [:header_context] warn '[TableStructure] `:header_context` option has been deprecated. Use `header: { context: ... }` option instead.' header = { context: header_context } end require 'csv' @options = { bom: bom, 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], **deprecated_options, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/table_structure/csv/writer.rb', line 40 def write( items, to:, bom: @options[:bom], csv_options: @options[:csv_options], **, &block ) header = @inner_options[:header] if .key?(:header) header = [:header] warn '[TableStructure] Specify :header option as an argument for initialize method.' end if .key?(:header_omitted) header_omitted = [:header_omitted] warn "[TableStructure] `header_omitted: #{!!header_omitted}` option has been deprecated. Use `header: #{!header_omitted}` option instead." header = !header_omitted end if .key?(:header_context) header_context = [:header_context] warn '[TableStructure] `:header_context` option has been deprecated. Use `header: { context: ... }` option instead.' header = { context: header_context } end = { header: header } to << BOM if bom csv = ::CSV.new(to, **) @writer.write(items, to: csv, **, &block) end |