Class: Hirb::Helpers::Table

### Home Classes Methods

### In Files

- lib/hirb/helpers/table/filters.rb
- lib/hirb/helpers/table/resizer.rb
- lib/hirb/helpers/table.rb

### Parent

Object

### Namespace

- MODULE Hirb::Helpers::Table::Filters
- CLASS Hirb::Helpers::Table::Resizer
- CLASS Hirb::Helpers::Table::TooManyFieldsForWidthError

### Methods

- ::render

### Files

- LICENSE.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Hirb
- Hirb::Console
- Hirb::DynamicView
- Hirb::Formatter
- Hirb::Helpers
- Hirb::Helpers::AutoTable
- Hirb::Helpers::ObjectTable
- Hirb::Helpers::ParentChildTree
- Hirb::Helpers::Table
- Hirb::Helpers::Table::Filters
- Hirb::Helpers::Table::Resizer
- Hirb::Helpers::Table::TooManyFieldsForWidthError
- Hirb::Helpers::Tree
- Hirb::Helpers::Tree::Node
- Hirb::Helpers::Tree::Node::MissingLevelError
- Hirb::Helpers::Tree::Node::MissingValueError
- Hirb::Helpers::Tree::ParentlessNodeError
- Hirb::Helpers::VerticalTable
- Hirb::Menu
- Hirb::Menu::Error
- Hirb::ObjectMethods
- Hirb::Pager
- Hirb::String
- Hirb::Util
- Hirb::View
- Hirb::Views

No matching classes.

# Hirb::Helpers::Table

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.

### Constants

BORDER_LENGTH
MIN_FIELD_LENGTH

### Attributes

filter_any[RW]

Boolean which sets the default for :filter_any option.

filter_classes[RW]

A hash which maps a cell value’s class to a filter. This serves to set a default filter per field if all of its values are a class in this hash. By default, Array values are comma joined and Hashes are inspected. See the :filter_any option to apply this filter per value.

last_table[RW]

Holds last table object created

### Public Class Methods

render(rows, options={}) click to toggle source

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. When set to false, headers are hidden. Can also be an array but only for array rows.

:max_fields

  A hash of fields and their maximum allowed lengths. Maximum length can also be a percentage of the total width (decimal less than one). When a field exceeds it’s maximum then it’s truncated and has a … appended to it. Fields that aren’t specified have no maximum.

:max_width

  The maximum allowed width of all fields put together including field borders. Only valid when :resize is true. Default is Hirb::View.width.

:resize

  Resizes table to display all columns in allowed :max_width. Default is true. Setting this false will display the full length of each field.

: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 their filters, applied to every row in a field. A filter can be a proc, an instance method applied to the field value or a Filters method. Also see the filter_classes attribute below.

:header_filter

  A filter, like one in :filters, that is applied to all headers after the :headers option.

:filter_any

  When set to true, any cell defaults to being filtered by its class in :filter_classes. Default Hirb::Helpers::Table.filter_any().

:filter_classes

  Hash which maps classes to filters. Default is Hirb::Helpers::Table.filter_classes().

: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.

:escape_special_chars

  When set to true, escapes special characters n,t,r 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]], :max_fields=>{0=>10}, :header_filter=>:capitalize
    Hirb::Helpers::Table.render [['a',1], ['b',2]], :change_fields=>%w{letters numbers}, :max_fields=>{'numbers'=>0.4}
    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]}

    # File lib/hirb/helpers/table.rb, line 100
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.