Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to write without headers #24

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/csb/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(output = '', items: [], **kwargs)

def build
output << UTF8_BOM if utf8_bom
output << CSV.generate_line(cols.headers, **csv_options)
output << CSV.generate_line(cols.headers, **csv_options) if write_headers?
items.each do |item|
output << CSV.generate_line(cols.values_by_item(item), **csv_options)
rescue => error
Expand All @@ -28,5 +28,11 @@ def build
end
output
end

private

def write_headers?
@csv_options.fetch(:write_headers, true)
end
end
end
6 changes: 6 additions & 0 deletions spec/lib/csb/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@

it { is_expected.to eq "Name\tEmail\tDummy\ntester1\[email protected]\t\ntester2\[email protected]\t\n" }
end

context 'without headers' do
let(:builder) { Csb::Builder.new(items: items, csv_options: { write_headers: false }) }

it { is_expected.to eq "tester1,[email protected],\ntester2,[email protected],\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 @@ -45,5 +45,17 @@

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

context 'without headers' do
subject { template.build }

let(:template) do
template = Csb::Template.new(streaming: false)
template.csv_options = { write_headers: false }
template
end

it { is_expected.to eq "tester1,[email protected],\ntester2,[email protected],\n" }
end
end
end