(re)-Introducing simple_importer

simple_importer has existed for some time as a lowly little gem that made it easy to create CSV import scripts using the Ruby CSV library. The API was usable but wasn't nearly as "simple" as it could be. Motivated by our upcoming Hackday event, Intro to Ruby Metaprogramming, I decided to rewrite simple_importer using some Ruby metaprogramming techniques. The end result is a gem that makes CSV import tasks easy to create and manage, and hopefully provides some real-world examples of basic Ruby metaprogramming.

The basic use case starts with defining importers using a small internal DSL. Each importer has a name, a csv file, and a block of code that is used to process each row in the file. The row instance that is passed into the block is a fastercsv row and all of the fastercsv configuration options are available.

 importer :items do file 'items.csv' foreach do |row| Item.create(:name => row[:name]) end end 

Once you have created an importer, save it into a directory named importers. This directory can be located in the same directory as your Rakefile, lib/importers or app/importers. Next, pull in the simple_importer rake tasks by adding the following line to your Rakefile.

 require 'simple_importer/tasks' 

Now, when you do a rake -T simple_importer you'll see a rake task for each importer as well as a rake task that will run all of the importers.

Pretty simple. Although, simple_importer does have a few other neat features such as before callbacks and multiple file processing capabilities. Be sure to check out the documentation which has plenty of examples. You can grab the gem from gemcutter, or check out the code on github.

Justin Marney

,
Posted in Article Category: #Code
on