Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tilo committed Jun 29, 2024
1 parent 628bf6d commit d542daa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

[![codecov](https://codecov.io/gh/tilo/smarter_csv/branch/main/graph/badge.svg?token=1L7OD80182)](https://codecov.io/gh/tilo/smarter_csv) [![Gem Version](https://badge.fury.io/rb/smarter_csv.svg)](http://badge.fury.io/rb/smarter_csv)

This library provides a complete interface to CSV files and data. It offers tools to enable you to read and write to and from Strings or IO objects, as needed.

#### LATEST CHANGES

Expand Down
1 change: 1 addition & 0 deletions lib/smarter_csv/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module SmarterCSV
class Generator
def initialize(file_path, options = {})
@options = options
@discover_headers = options.has_key?(:discover_headers) ? (options[:discover_headers] == true) : true
@headers = options[:headers] || []
@col_sep = options[:col_sep] || ','
@force_quotes = options[:force_quotes]
Expand Down
4 changes: 2 additions & 2 deletions smarter_csv.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
spec.authors = ["Tilo Sloboda"]
spec.email = ["[email protected]"]

spec.summary = "Ruby Gem for smarter importing of CSV Files (and CSV-like files), with lots of optional features, e.g. chunked processing for huge CSV files"
spec.description = "Ruby Gem for smarter importing of CSV Files as Array(s) of Hashes, with optional features for processing large files in parallel, embedded comments, unusual field- and record-separators, flexible mapping of CSV-headers to Hash-keys"
spec.summary = "CSV Reading and Writing"
spec.description = "Ruby Gem for smarter importing of CSV Files as Array(s) of Hashes, with lots of features for processing large files in parallel, embedded comments, unusual field- and record-separators, flexible mapping of CSV-headers to Hash-keys"
spec.homepage = "https://github.com/tilo/smarter_csv"
spec.license = 'MIT'

Expand Down
62 changes: 23 additions & 39 deletions spec/smarter_csv/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,35 @@
File.delete(file_path) if File.exist?(file_path)
end

context 'when headers are given in advance' do
let(:options) { { headers: %w[name age city] } }
let(:data_batches) do
let(:data_batches) do
[
[
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, country: 'USA' }
],
[
[
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, country: 'USA' }
],
[
{ name: 'Mike', age: 35, city: 'Chicago', state: 'IL' }
]
{ name: 'Mike', age: 35, city: 'Chicago', state: 'IL' }
]
end
]
end

context 'when headers are given in advance' do
let(:options) { { headers: %w[name age city] } }

it 'writes the given headers and data correctly' do
generator = SmarterCSV::Generator.new(file_path, options)
data_batches.each { |batch| generator.append(batch) }
generator.finalize

output = File.read(file_path)
expect(output).to include("name,age,city\n")
expect(output).to include("John,30,New York\n")
expect(output).to include("Jane,25,\n")
expect(output).to include("Mike,35,Chicago\n")
expect(output).to include("name,age,city,country,state\n")
expect(output).to include("John,30,New York,,\n")
expect(output).to include("Jane,25,,USA,\n")
expect(output).to include("Mike,35,Chicago,,IL\n")
end
end

context 'when headers are automatically discovered' do
let(:data_batches) do
[
[
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, country: 'USA' }
],
[
{ name: 'Mike', age: 35, city: 'Chicago', state: 'IL' }
]
]
end

it 'writes the discovered headers and data correctly' do
generator = SmarterCSV::Generator.new(file_path)
data_batches.each { |batch| generator.append(batch) }
Expand All @@ -63,20 +52,15 @@
context 'when headers are mapped' do
let(:options) do
{
map_headers: { name: 'Full Name', age: 'Age', city: 'City', country: 'Country', state: 'State' }
map_headers: {
name: 'Full Name',
age: 'Age',
city: 'City',
country: 'Country',
state: 'State',
}
}
end
let(:data_batches) do
[
[
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, country: 'USA' }
],
[
{ name: 'Mike', age: 35, city: 'Chicago', state: 'IL' }
]
]
end

it 'writes the mapped headers and data correctly' do
generator = SmarterCSV::Generator.new(file_path, options)
Expand Down

0 comments on commit d542daa

Please sign in to comment.