<% $:.unshift File.expand_path(File.dirname(__FILE__), "/../lib/table_fu") require 'rubygems' require 'table_fu' require 'uv' def code_for(file, output=true) return '' unless File.exists?("examples/#{file}.rb") file = File.open("examples/#{file}.rb").read html = Uv.parse(file, "xhtml", "ruby", false, "dawn", false) out = '' if output csv =<<-CSV Author,Best Book,Number of Pages,Style Samuel Beckett,Malone Muert,120,Modernism James Joyce,Ulysses,644,Modernism Nicholson Baker,Mezannine,150,Minimalism Vladimir Sorokin,The Queue,263,Satire CSV if block_given? out = yield instance_eval(file) else out = instance_eval(file).inspect end out = "Returns:" + Uv.parse(out, 'xhtml', 'ruby', false, 'dawn', false) end "#{html}#{out}" end %> TableFu  

TableFu – Version: <%= config = YAML.load(File.read('VERSION.yml')) "#{config[:major]}.#{config[:minor]}.#{config[:patch]}" %>

TableFu is a ruby gem for spreadsheet-style handling of arrays (e.g. filtering, formatting, and sorting by "column" or "row"). In addition, it has the ability to facet — or group — rows according to cell value. It was developed as a backend for its companion project TableSetter.

For example, TableFu can consume a csv file and sort on a column: <%= code_for "sort_by_column" do |ret| ret.rows.inspect end %>

Table of Contents

Installation

TableFu is available as a rubygem:

 gem install table_fu
or from the actual source:
 git clone git://github.com/propublica/table-fu.git
 cd table-fu
 rake install

Usage

The TableFu constructor takes two arguments; a 2 dimensional array or csv (file object or string) and a hash of column options or a block. TableFu will assume that the first row of the array contains the column headers. The simple options are:

sorted_by: the column to sort by, it defaults to no sorting at all. <%= code_for "sort_by_column" do |ret| ret.rows.inspect end %>

columns: the columns to include in the table, useful when reordering and filtering extraneous columns. If no arguments are provided, TableFu will include all columns by default. <%= code_for "columns" %> Note that the columns are still accessible directly even if they're not in the columns array. <%= code_for "columns_hidden" %>

Macros / Formatting

TableFu allows you to format columns of cells through the use of macros. See TableFu::Formatting for the predefined macros available.

The formatting attribute should be a hash of the form: <%= code_for "formatting_options", false %> which will call the macro on the column name with the arguments if specified.

For example, you can use the last_name formatter to extract the last name of a cell containing a person's name: <%= code_for "last_name" %>

Of course, you can provide your own macros by patching TableFu::Formatting. TableSetter includes rails view helpers directly in TableFu::Formatting. <%= code_for "rails_helpers", false %>

Faceting

Faceting provides a way to group rows together using a cell value they share in common. Calling TableFu#faceted_by returns an array of table fu instances each with a faceted_on attribute and with only the rows where that value appears.

In this example there are 2 rows where "Modernism" appears in the style column, so calling faceted_on with the argument "Style" returns a TableFu instance with those rows grouped together: <%= code_for "faceting" do |s| s.map { |table| "table.faceted_on => #{table.faceted_on}, table.rows => #{table.rows.inspect}\n" }.join('') end %>

Manipulation / Output

Deleting Rows

In addition to hiding columns and faceting TableFu can delete rows from the csv. Let's get rid of James Joyce (no one ever finished Ulysses anyway): <%= code_for "zap_joyce" do |s| s.rows.inspect end %> The deleted rows are still available in @deleted_rows for later access: <%= code_for "zap_joyce" do |s| "table.deleted_rows => #{s.deleted_rows.inspect}" end %>

Pagination

If you want only a chunk of the data, say to paginate your table, you can call only! with the range of values you want to keep: <%= code_for "only" %>

Sum

TableFu can also sum a column of values: <%= code_for "totals" %>

Links

Credits

Jeff Larson (Maintainer), Brian Boyer, Scott Klein, Mark Percival, Charles Brian Quinn, and James McKinney.

.

License

<%= File.open("LICENSE").read %>