module Thamble

Thamble exposes a single method named table to make it easy to generate an HTML table from an enumerable.

Constants

OPTS

Empty frozen hash used for default option hashes

Public Class Methods

table(enum, opts=OPTS) { |row, t| ... } click to toggle source

Return a string containing an HTML table representing the data from the enumerable. enum should be an enumerable containing the data. If a block is given, it is yielded each element in enum, and should return an array representing the table data to use for that row.

Options:

:caption

A caption for the table

:column_th

Use th instead of td for the first cell in each row in the body.

:headers

The headers to use for the table, as an array of strings or a single string using commas as the separtor.

:table

HTML attribute hash for the table itself

:td

HTML attribute hash for the table data cells, can be a proc called with the data value, row position, and row that returns a hash

:th

HTML attribute hash for the table header cells, can be a proc called with the header that returns a hash

:tr

HTML attribute hash for the table rows, can be a proc called with the row that returns a hash

:widths

An array of widths to use for each column

   # File lib/thamble.rb
29 def table(enum, opts=OPTS)
30   t = Table.new(opts)
31   enum.each do |row|
32     row = yield(row, t) if block_given?
33     t << row
34   end
35   t.to_s
36 end

Private Instance Methods

table(enum, opts=OPTS) { |row, t| ... } click to toggle source

Return a string containing an HTML table representing the data from the enumerable. enum should be an enumerable containing the data. If a block is given, it is yielded each element in enum, and should return an array representing the table data to use for that row.

Options:

:caption

A caption for the table

:column_th

Use th instead of td for the first cell in each row in the body.

:headers

The headers to use for the table, as an array of strings or a single string using commas as the separtor.

:table

HTML attribute hash for the table itself

:td

HTML attribute hash for the table data cells, can be a proc called with the data value, row position, and row that returns a hash

:th

HTML attribute hash for the table header cells, can be a proc called with the header that returns a hash

:tr

HTML attribute hash for the table rows, can be a proc called with the row that returns a hash

:widths

An array of widths to use for each column

   # File lib/thamble.rb
29 def table(enum, opts=OPTS)
30   t = Table.new(opts)
31   enum.each do |row|
32     row = yield(row, t) if block_given?
33     t << row
34   end
35   t.to_s
36 end