Turn objects into nicely formatted columns for easy reading
Pull request Compare This branch is 1 commit ahead, 230 commits behind arches:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Turn objects into nicely formatted columns for easy reading

TablePrint formats an object or array of objects into columns for easy reading. To do this, it assumes the objects in your array all respond to the same methods.


# Install as a standalone gem
$ gem install table_print

# Install within rails
In your Gemfile: gem 'table_print'
$ bundle install


# Outside rails
$ irb
> require 'table_print'
> tp array_of_objects, options = {}

# Inside rails, the gem has already been required by your Gemfile so all you need to do is
$ rails c
> tp array_of_objects, options = {}

You should see something like this:

NAME              | SUMMARY                         | TITLE
Michael Connelly  | Another book by Michael Con...  | The Fifth Witness
Manning Mardale   | From acclaimed historian Ma...  | Malcolm X
Tina Fey          | Worth it. -Trees                | Bossypants

TablePrint tries to use sensible defaults to choose the columns to show. If you're inspecting ActiveRecord objects, it uses the ActiveRecord column names. You can customize the output to show fewer columns, or show other methods you've written on your model.

# Maybe you store a user's hourly rate but you want to see their yearly income
> tp User.limit(30), {:include => :yearly_income, :except => :hourly_rate}

# Maybe all you care about is their mailing info
> tp User.limit(30), {:only => [:address, :city, :state, :zip]}

If you're not using ActiveRecord, the TablePrint default is to show all the methods on your object. Thus, the :include option is useless at the moment. You are still able to use :only and :except.

You can reference nested objects with the method chain required to reach them. Say you had some users who wrote books, and those books had photos.

> tp array_of_objects, :only => ["name", "books.title", "books.photos.caption"]

NAME              | BOOKS > TITLE     | BOOKS > PHOTOS > CAPTION
Michael Connelly  |                   |
                  | The Fifth Witness |
                  |                   | Susan was running, fast, away...
                  |                   | Along came a spider.
                  | Malcolm X         |
                  | Bossypants        |
                  |                   | Yes! Yes! A thousand times ye...
                  |                   | Don't see many like you aroun...
Carrot Top        |                   |
Milton Greene     |                   |
                  | How I Learned     |
                  |                   | Once upon a time, I was a sma...
                  |                   | Lemons are yellow, limes are ...
                  |                   | Never as a woman her age. I l...
                  | Desperados        |
                  |                   | Avast.
                  |                   | Giraffes lived a peaceful exi...

Column options

Pass options to individual columns through the options hash by using the display method as the hash key. Eg, if you wanted to rename the pay_rate column to Special User Payment Teir,

tp User.all, :pay_rate => {:name => "Special User Payment Teir"}

Columns have other options, including:

name: Use this in the column header instead of the name of the display method.

max_field_length: (default: 30) Field lengths are determined based on the data in the column. Setting your own max will ensure that the column is as skinny as possible, but never above the number you provide.

field_length: Useful for very large data sets, this option will explicitly set the column width regardless of the data it contains. The max_field_length option takes precedence over field_length - ie, field_length can't be longer than max_field_length.

Contributing to table_print

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.


Copyright © 2011 Chris Doyle. See LICENSE.txt for further details.