<img src=“https://secure.travis-ci.org/arches/table_print.png” />
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. Use symbols or strings to reference the columns.
# Maybe you story 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), :address, 'city', 'state', :zip
If you’re not using ActiveRecord, the TablePrint default is to show all the methods defined directly on your object (nothing from superclasses/mixins).
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, "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...
Pass options to individual columns through the options hash by using the display method as the hash key. Eg, if you wanted a skinny email column, set the width explicitly:
tp User.all, :email => {:width => 12}
-
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.