@alexbush we run into CSV parsing and generating pretty frequently. I've used the built-in CSV support from Ruby for the past few years, and it's held up pretty well. There are a few things I find helpful when dealing with it:
- Use a dedicated parser class that encapsulates CSV parsing. This also serves as a good place to collect errors.
CSV.parse handles parse errors, but you can make them prettier.
- Avoid calling
to_csv or anything like that on collections. It inevitably results in trying to generate CSV for a huge collection, resulting in memory bloat. Do it row-by-row instead.
- Try to do both parsing and generating in background jobs, as large CSV files will slow down your HTML requests.
If there are any specific examples or questions, feel free to start a new thread on the subject.
@danscotton I agree - if you inject the dependency, you can totally hide the fact that parsing is going on altogether. I would probably make the
Parser implement the
Enumerable interface and then just accept a
recipients parameter in
initialize. Then, you can pass an
Parser, or the results from a database query, and
Sender doesn't have to change at all, even semantically.
@Rafael_George This particular example actually doesn't actually use Rails! The
spec_helper.rb I was using in this example just loaded
active_support, and files from
In general, I haven't found a lot of benefit from having separate
spec_helper.rb files, even in Rails projects. There are two potential benefits to a slimmer
- Speed: loading Rails and its dependencies takes a while. However, using something like spring largely removes this concern.
- Isolation: loading Rails makes it harder to examine your dependencies. Unfortunately, just having ActiveSupport (necessary for most framework classes like models, views, controllers, or mailers) is enough to bring auto loading into the picture. Because Ruby doesn't isolate files from each other - a file
required from one location is available everywhere - and because ActiveSupport automatically tries to load dependencies anyway, I find that you really can't have dependency isolation in a Rails project. Attempting to do so just provides a false sense of security, as there's no way to enforce the isolation - you never know where your dependencies are coming from.