Skip to content

Instantly share code, notes, and snippets.

@dogweather
Created May 7, 2014 19:50
Show Gist options
  • Save dogweather/1479664d12d0c7c27492 to your computer and use it in GitHub Desktop.
Save dogweather/1479664d12d0c7c27492 to your computer and use it in GitHub Desktop.
A first pass at a CSV API which provides header-based access plus error handling
describe BetterCsv do
it 'has a nice API' do
parser = BetterCsv.parse('info.csv', headers: :first_row)
parser.each_row do |parse_result|
next if parse_result.error?
puts "Found the name and phone #: #{parse_result.name}, #{parse_result.telephone}"
end
end
end
@dogweather
Copy link
Author

This could have a second way to check for errors, throwing an exception instead of returning a boolean. Similar to ActiveRecord#save vs. ActiveRecord#save!

E.g., instead of using parse_result.error? the user of the library could choose to call parse_result.validate! which would throw the CSV parsing error, else take no action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment