Build Status Code Climate

Bend CSVs to your will with declarative schemas. Map one or many columns, preprocess cells and lazily enumerate. Declarative schemas are easier to understand, quicker to setup and independent of I/O. Use CSV (Formally FasterCSV), Spreadsheet or any array of array-like data structure.

Quick and Dirty Examples

Open a CSV file and declare a schema. A schema compromises of columns. A column takes an arbitrary name followed by its position in the input. A column may be derived from multiple positions.

``` ruby require ‘conformist’ require ‘csv’

csv = ‘~/transmitters.csv’ schema = do column :callsign, 1 column :latitude, 1, 2, 3 column :longitude, 3, 4, 5 column :name, 0 do |value| value.upcase end end ```

Insert the transmitters into a SQLite database.

``` ruby require ‘sqlite3’

db = ‘transmitters.db’ schema.conform(csv).each do |transmitter| db.execute “INSERT INTO transmitters (callsign, …) VALUES (‘#transmittertransmitter.callsign’, …);” end ```

Only insert the transmitters with the name “Mount Cooth-tha” using ActiveRecord or DataMapper.

ruby transmitters = schema.conform(csv).select do |transmitter| == 'Mount Coot-tha' end transmitters.each do |transmitter| Transmitter.create! transmitter.attributes end

Source from multiple, different input files and insert transmitters together into a single database.

``` ruby require ‘conformist’ require ‘csv’ require ‘sqlite3’

au_schema = do column :callsign, 8 column :latitude, 10 end us_schema = do column :callsign, 1 column :latitude, 1, 2, 3 end

au_csv = ‘~/au/transmitters.csv’ us_csv = ‘~/us/transmitters.csv’

db = ‘transmitters.db’

[au_schema.conform(au_csv), us_schema.conform(us_csv)].each do |schema| schema.each do |transmitter| db.execute “INSERT INTO transmitters (callsign, …) VALUES (‘#transmittertransmitter.callsign’, …);” end end ```

Open a Microsoft Excel spreadsheet and declare a schema.

``` ruby require ‘conformist’ require ‘spreadsheet’

book = ‘~/states.xls’ sheet = book.worksheet 0 schema = do column :state, 0, 1 do |values| “#valuesvalues.first, #valuesvalues.last” end column :capital, 2 end ```

Print each state’s attributes to standard out.

ruby schema.conform(sheet).each do |state| $stdout.puts state.attributes end

For more examples see test/fixtures, test/schemas and test/unit/integration_test.rb.


Conformist is available as a gem. Install it at the command line.

sh $ [sudo] gem install conformist

Or add it to your Gemfile and run $ bundle install.

ruby gem 'conformist'


Anonymous Schema

Anonymous schemas are quick to declare and don’t have the overhead of creating an explicit class.

``` ruby citizen = do column :name, 0, 1 column :email, 2 end

citizen.conform [[‘Tate’, ‘Johnson’, ‘’]] ```

Class Schema

Class schemas are explicit. Class schemas were the only type available in earlier versions of Conformist.

``` ruby class Citizen extend Conformist

column :name, 0, 1 column :email, 2 end

Citizen.conform [[‘Tate’, ‘Johnson’, ‘’]] ```

Implicit Indexing

Column indexes are implicitly incremented when the index argument is omitted. Implicit indexing is all or nothing.

ruby column :account_number # => 0 column :date { |v| *v.split('/').reverse } # => 1 column :description # => 2 column :debit # => 3 column :credit # => 4


Conform is the principle method for lazily applying a schema to the given input.

ruby enumerator = schema.conform'~/file.csv') enumerator.each do |row| puts row.attributes end


#conform expects any object that responds to #each to return an array-like object.

ruby'~/file.csv').responds_to? :each # => true [[], [], []].responds_to? :each # => true

Header Row

#conform takes an option to skip the first row of input. Given a typical CSV document, the first row is the header row and irrelevant for enumeration.

ruby schema.conform'~/file_with_headers.csv'), :skip_first => true

Named Columns

Strings can be used as column indexes instead of integers. These strings will be matched against the first row to determine the appropriate numerical index.

``` ruby citizen = do column :email, ‘EM’ column :name, ‘FN’, ‘LN’ end

citizen.conform [[‘FN’, ‘LN’, ‘EM’], [‘Tate’, ‘Johnson’, ‘’]], :skip_first => true ```


#conform is lazy, returning an Enumerator. Input is not parsed until you call #each, #map or any method defined in Enumerable. That means schemas can be assigned now and evaluated later. #each has the lowest memory footprint because it does not build a collection.


The argument passed into the block is a struct-like object. You can access columns as methods or keys. Columns were only accessible as keys in earlier versions of Conformist. Methods are now the preferred syntax.

ruby citizen[:name] # => "Tate Johnson" # => "Tate Johnson"

For convenience the #attributes method returns a hash of key-value pairs suitable for creating ActiveRecord or DataMapper records.

ruby citizen.attributes # => {:name => "Tate Johnson", :email => ""}

One Column

Maps the first column in the input file to :first_name. Column indexing starts at zero.

ruby column :first_name, 0

Many Columns

Maps the first and second columns in the input file to :name.

ruby column :name, 0, 1

Indexing is completely arbitrary and you can map any combination.

ruby column :name_and_city 0, 1, 2

Many columns are implicitly concatenated. Behaviour can be changed by passing a block. See preprocessing.


Sometimes values need to be manipulated before they’re conformed. Passing a block gets access to values. The return value of the block becomes the conformed output.

ruby column :name, 0, 1 do |values| * ' ' end

Works with one column too. Instead of getting a collection of objects, one object is passed to the block.

ruby column :first_name, 0 do |value| value.upcase end

It’s also possible to provide a context object that is made available during preprocessing.

``` ruby citizen = do column :name, 0, 1 do |values, context| (context[:upcase?] ? : values) * ‘ ‘ end end

citizen.conform [[‘tate’, ‘johnson’]], context: true ```

Virtual Columns

Virtual columns are not sourced from input. Omit the index to create a virtual column. Like real columns, virtual columns are included in the conformed output.

ruby column :day do 1 end


Inheriting from a schema gives access to all of the parent schema’s columns.

Anonymous Schema

Anonymous inheritance takes inspiration from Ruby’s syntax for instantiating new classes.

``` ruby parent = do column :name, 0, 1 end

child = parent do column :category do ‘Child’ end end ```

Class Schema

Classical inheritance works as expected.

``` ruby class Parent extend Conformist

column :name, 0, 1 end

class Child < Parent column :category do ‘Child’ end end ```

Upgrading from <= 0.0.3 to >= 0.1.0

Where previously you had

``` ruby class Citizen include Conformist::Base

column :name, 0, 1 end

Citizen.load(‘~/file.csv’).foreach do |citizen| # … end ```

You should now do

``` ruby require ‘fastercsv’

class Citizen extend Conformist

column :name, 0, 1 end

Citizen.conform(‘~/file.csv’)).each do |citizen| # … end ```

See for a full list of changes.


  • MRI 2.4.0, 2.3.1, 2.2.0, 2.1.0, 2.0.0, 1.9.3
  • JRuby


No explicit dependencies, although CSV and Spreadsheet are commonly used.


  1. Fork
  2. Install dependancies by running $ bundle install
  3. Write tests and code
  4. Make sure the tests pass locally by running $ bundle exec rake
  5. Push to GitHub and make sure continuous integration tests pass at
  6. Send a pull request on GitHub

Please do not increment the version number in lib/conformist/version.rb. The version number will be incremented by the maintainer after the patch is accepted.


Motivation for this project came from the desire to simplify importing data from various government organisations into Antenna Mate. The data from each government was similar, but had completely different formatting. Some pieces of data needed preprocessing while others simply needed to be concatenated together. Not wanting to write a parser for each new government organisation, I created Conformist.

Copyright © 2016 Tate Johnson. Conformist is released under the MIT license. See LICENSE for details.