= table_print
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.
== Installation
# Install as a standalone gem
$ gem install table_print
# Install within rails
In your Gemfile: gem 'table_print'
$ bundle install
== Usage
# 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 = {}
And 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 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), {: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 :include.
=== 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.
== 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
Copyright (c) 2011 Chris Doyle. See LICENSE.txt for further details.