Class Hirb::Helpers::Table

  1. lib/hirb/helpers/table.rb
Parent: Object

Base Table class from which other table classes inherit. By default, a table is constrained to a default width but this can be adjusted via the max_width option or Hirb::View.width. Rows can be an array of arrays or an array of hashes.

An array of arrays ie [[1,2], [2,3]], would render:

+---+---+
| 0 | 1 |
+---+---+
| 1 | 2 |
| 2 | 3 |
+---+---+

By default, the fields/columns are the numerical indices of the array.

An array of hashes ie [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], would render:

+-----+--------+
| age | weight |
+-----+--------+
| 10  | 100    |
| 80  | 500    |
+-----+--------+

By default, the fields/columns are the keys of the first hash.

Custom Callbacks

Callback methods can be defined to add your own options that modify rows right before they are rendered. Here’s an example that allows for searching with a :query option:

module Query
  # Searches fields given a query hash
  def query_callback(rows, options)
    return rows unless options[:query]
    options[:query].map {|field,query|
      rows.select {|e| e[field].to_s =~ /#{query}/i }
    }.flatten.uniq
  end
end
Hirb::Helpers::Table.send :include, Query

>> puts Hirb::Helpers::Table.render [{:name=>'batman'}, {:name=>'robin'}], :query=>{:name=>'rob'}
+-------+
| name  |
+-------+
| robin |
+-------+
1 row in set

Callback methods:

  • must be defined in Helpers::Table and end in ‘_callback’.
  • should expect rows and a hash of render options. Rows will be an array of hashes.
  • are expected to return an array of hashes.
  • are invoked in alphabetical order.

For a thorough example, see Boson::Pipe.

Methods

public class

  1. render

Constants

BORDER_LENGTH = 3

Public class methods

render (rows, options={})

Main method which returns a formatted table.

Options:

:fields
An array which overrides the default fields and can be used to indicate field order.
:headers
A hash of fields and their header names. Fields that aren’t specified here default to their name. This option can also be an array but only for array rows.
:field_lengths
A hash of fields and their maximum allowed lengths. If a field exceeds it’s maximum length than it’s truncated and has a … appended to it. Fields that aren’t specified here have no maximum allowed length.
:max_width
The maximum allowed width of all fields put together. This option is enforced except when the field_lengths option is set. This doesn’t count field borders as part of the total.
:number
When set to true, numbers rows by adding a :hirb_number column as the first column. Default is false.
:change_fields
A hash to change old field names to new field names. This can also be an array of new names but only for array rows. This is useful when wanting to change auto-generated keys to more user-friendly names i.e. for array rows.
:filters
A hash of fields and the filters that each row in the field must run through. The filter converts the cell’s value by applying a given proc or an array containing a method and optional arguments to it.
:vertical
When set to true, renders a vertical table using Hirb::Helpers::VerticalTable. Default is false.
:all_fields
When set to true, renders fields in all rows. Valid only in rows that are hashes. Default is false.
:description
When set to true, renders row count description at bottom. Default is true.
:no_newlines
When set to true, stringifies newlines so they don’t disrupt tables. Default is false for vertical tables and true for anything else.

Examples:

Hirb::Helpers::Table.render [[1,2], [2,3]]
Hirb::Helpers::Table.render [[1,2], [2,3]], :field_lengths=>{0=>10}
Hirb::Helpers::Table.render [['a',1], ['b',2]], :change_fields=>%w{letters numbers}
Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}]
Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :headers=>{:weight=>"Weight(lbs)"}
Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :filters=>{:age=>[:to_f]}
[show source]
# File lib/hirb/helpers/table.rb, line 90
    def render(rows, options={})
      options[:vertical] ? Helpers::VerticalTable.render(rows, options) : new(rows, options).render
    rescue TooManyFieldsForWidthError
      $stderr.puts "", "** Error: Too many fields for the current width. Configure your width " +
        "and/or fields to avoid this error. Defaulting to a vertical table. **"
      Helpers::VerticalTable.render(rows, options)
    end