Skip to content

Commit

Permalink
Merge pull request #18 from kzkn/generate-line-options
Browse files Browse the repository at this point in the history
Add `csv_options` as an option to pass to `CSV.generate_line`
  • Loading branch information
aki77 authored Apr 12, 2024
2 parents a1c99e4 + c90dc6c commit acd298d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
9 changes: 5 additions & 4 deletions lib/csb/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ module Csb
class Builder
UTF8_BOM = "\xEF\xBB\xBF".freeze

attr_reader :output, :utf8_bom, :items, :cols
attr_reader :output, :utf8_bom, :items, :cols, :csv_options
attr_accessor :items

def initialize(output = '', items: [], utf8_bom: false)
def initialize(output = '', items: [], utf8_bom: false, csv_options: {})
@output = output
@utf8_bom = utf8_bom
@cols = Cols.new
@items = items
@csv_options = csv_options
end

def build
output << UTF8_BOM if utf8_bom
output << CSV.generate_line(cols.headers)
output << CSV.generate_line(cols.headers, **csv_options)
items.each do |item|
output << CSV.generate_line(cols.values_by_item(item))
output << CSV.generate_line(cols.values_by_item(item), **csv_options)
rescue => error
break if Csb.configuration.ignore_class_names.include?(error.class.name)

Expand Down
7 changes: 4 additions & 3 deletions lib/csb/template.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module Csb
class Template
attr_accessor :utf8_bom, :filename, :streaming, :items, :cols
attr_accessor :utf8_bom, :filename, :streaming, :items, :cols, :csv_options

def initialize(utf8_bom:, streaming:)
@utf8_bom = utf8_bom
@streaming = streaming
@cols = Cols.new
@items = []
@csv_options = {}
end

def build
Expand All @@ -20,15 +21,15 @@ def streaming?
private

def build_string
builder = Builder.new(utf8_bom: utf8_bom, items: items)
builder = Builder.new(utf8_bom: utf8_bom, items: items, csv_options: csv_options)
builder.cols.copy!(cols)
builder.build
end

def build_enumerator
Enumerator.new do |y|
begin
builder = Builder.new(y, utf8_bom: utf8_bom, items: items)
builder = Builder.new(y, utf8_bom: utf8_bom, items: items, csv_options: csv_options)
builder.cols.copy!(cols)
builder.build
rescue => error
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/csb/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@

it { is_expected.to eq "\xEF\xBB\xBFName,Email,Dummy\ntester1,[email protected],\ntester2,[email protected],\n" }
end

context 'with csv_options' do
let(:builder) { Csb::Builder.new(items: items, csv_options: { col_sep: "\t" }) }

it { is_expected.to eq "Name\tEmail\tDummy\ntester1\t[email protected]\t\ntester2\t[email protected]\t\n" }
end
end
end
12 changes: 12 additions & 0 deletions spec/lib/csb/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,17 @@

it { is_expected.to eq "Name,Email,Dummy\ntester1,[email protected],\ntester2,[email protected],\n" }
end

context 'with csv_options' do
subject { template.build }

let(:template) { Csb::Template.new(streaming: false, utf8_bom: false) }

before do
template.csv_options = { row_sep: "\r\n" }
end

it { is_expected.to eq "Name,Email,Dummy\r\ntester1,[email protected],\r\ntester2,[email protected],\r\n" }
end
end
end

0 comments on commit acd298d

Please sign in to comment.