Parent

Prawn::Table::Cells

Represents a selection of cells to be styled. Operations on a CellProxy can be chained, and cell properties can be set one-for-all on the proxy.

To set vertical borders only:

table.cells.borders = [:left, :right]

To highlight a rectangular area of the table:

table.rows(1..3).columns(2..4).background_color = 'ff0000'

Public Instance Methods

[](row, col) click to toggle source

Retrieves a cell based on its 0-based row and column. Returns an individual Cell, not a Cells collection.

table.cells[0, 0].content # => "First cell content"
# File lib/prawn/table/cells.rb, line 89
def [](row, col)
  find { |c| c.row == row && c.column == col }
end
column(col_spec) click to toggle source
Alias for: columns
columns(col_spec) click to toggle source

Limits selection to the given column or columns. col_spec can be anything that responds to the === operator selecting a set of 0-based column numbers; most commonly a number or a range.

table.column(0)     # selects first column
table.columns(3..4) # selects columns four and five
# File lib/prawn/table/cells.rb, line 68
def columns(col_spec)
  index_cells unless @indexed
  col_spec = transform_spec(col_spec, @column_count)
  Cells.new(@columns[col_spec] ||= select{ |c| col_spec === c.column })
end
Also aliased as: column
filter(&block) click to toggle source

Allows you to filter the given cells by arbitrary properties.

table.column(4).filter { |cell| cell.content =~ /Yes/ }.
  background_color = '00ff00'
# File lib/prawn/table/cells.rb, line 80
def filter(&block)
  Cells.new(select(&block))
end
height() click to toggle source

Returns the total height of all rows in the selected set.

# File lib/prawn/table/cells.rb, line 146
def height
  row_heights = {}
  each do |cell| 
    row_heights[cell.row] = 
      [row_heights[cell.row], cell.height].compact.max
  end
  row_heights.values.inject(0) { |sum, width| sum + width }
end
max_width() click to toggle source

Returns maximum width that can contain cells in the set.

# File lib/prawn/table/cells.rb, line 135
def max_width
  column_max_widths = {}
  each do |cell| 
    column_max_widths[cell.column] = 
      [column_max_widths[cell.column], cell.max_width].compact.min
  end
  column_max_widths.values.inject(0) { |sum, width| sum + width }
end
method_missing(id, *args, &block) click to toggle source

Supports setting arbitrary properties on a group of cells.

table.cells.row(3..6).background_color = 'cc0000'
# File lib/prawn/table/cells.rb, line 159
def method_missing(id, *args, &block)
  each { |c| c.send(id, *args, &block) }
end
min_width() click to toggle source

Returns minimum width required to contain cells in the set.

# File lib/prawn/table/cells.rb, line 124
def min_width
  column_min_widths = {}
  each do |cell| 
    column_min_widths[cell.column] = 
      [column_min_widths[cell.column], cell.min_width].compact.max
  end
  column_min_widths.values.inject(0) { |sum, width| sum + width }
end
row(row_spec) click to toggle source
Alias for: rows
rows(row_spec) click to toggle source

Limits selection to the given row or rows. row_spec can be anything that responds to the === operator selecting a set of 0-based row numbers; most commonly a number or a range.

table.row(0)     # selects first row
table.rows(3..4) # selects rows four and five
# File lib/prawn/table/cells.rb, line 54
def rows(row_spec)
  index_cells unless @indexed
  row_spec = transform_spec(row_spec, @row_count)
  Cells.new(@rows[row_spec] ||= select{ |c| row_spec === c.row })
end
Also aliased as: row
style(options={}, &block) click to toggle source

Supports setting multiple properties at once.

table.cells.style(:padding => 0, :border_width => 2)

is the same as:

table.cells.padding = 0
table.cells.border_width = 2

You can also pass a block, which will be called for each cell in turn. This allows you to set more complicated properties:

table.cells.style { |cell| cell.border_width += 12 }
# File lib/prawn/table/cells.rb, line 107
def style(options={}, &block)
  each { |cell| cell.style(options, &block) }
end
width() click to toggle source

Returns the total width of all columns in the selected set.

# File lib/prawn/table/cells.rb, line 113
def width
  column_widths = {}
  each do |cell| 
    column_widths[cell.column] = 
      [column_widths[cell.column], cell.width].compact.max
  end
  column_widths.values.inject(0) { |sum, width| sum + width }
end

Protected Instance Methods

index_cells() click to toggle source

Defers indexing until rows() or columns() is actually called on the Cells object. Without this, we would needlessly index the leaf nodes of the object graph, the ones that are only there to be iterated over.

Make sure to call this before using @rows or @columns.

# File lib/prawn/table/cells.rb, line 171
def index_cells
  @rows = {}
  @columns = {}

  each do |cell|
    @rows[cell.row] ||= []
    @rows[cell.row] << cell

    @columns[cell.column] ||= []
    @columns[cell.column] << cell
  end

  @row_count    = @rows.size
  @column_count = @columns.size

  @indexed = true
end
transform_spec(spec, total) click to toggle source

Transforms spec, a column / row specification, into an object that can be compared against a row or column number using ===. Normalizes negative indices to be positive, given a total size of total.

# File lib/prawn/table/cells.rb, line 193
def transform_spec(spec, total)
  case spec
  when Range
    transform_spec(spec.begin, total)..transform_spec(spec.end, total)
  when Integer
    spec < 0 ? (total + spec) : spec
  else # pass through
    spec
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.