Class: Prawn::Table::Cells

Inherits:
Array
  • Object
show all
Defined in:
lib/prawn/table/cells.rb

Overview

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'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object

Supports setting arbitrary properties on a group of cells.

table.cells.row(3..6).background_color = 'cc0000'


160
161
162
# File 'lib/prawn/table/cells.rb', line 160

def method_missing(id, *args, &block)
  each { |c| c.send(id, *args, &block) }
end

Instance Method Details

#[](row, col) ⇒ Object

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"


90
91
92
# File 'lib/prawn/table/cells.rb', line 90

def [](row, col)
  find { |c| c.row == row && c.column == col }
end

#columns(col_spec) ⇒ Object Also known as: column

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


69
70
71
72
73
# File 'lib/prawn/table/cells.rb', line 69

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

#filter(&block) ⇒ Object

Allows you to filter the given cells by arbitrary properties.

table.column(4).filter { |cell| cell.content =~ /Yes/ }.
  background_color = '00ff00'


81
82
83
# File 'lib/prawn/table/cells.rb', line 81

def filter(&block)
  Cells.new(select(&block))
end

#heightObject

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



147
148
149
150
151
152
153
154
# File 'lib/prawn/table/cells.rb', line 147

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_widthObject

Returns maximum width that can contain cells in the set.



136
137
138
139
140
141
142
143
# File 'lib/prawn/table/cells.rb', line 136

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

#min_widthObject

Returns minimum width required to contain cells in the set.



125
126
127
128
129
130
131
132
# File 'lib/prawn/table/cells.rb', line 125

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

#rows(row_spec) ⇒ Object Also known as: row

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


55
56
57
58
59
# File 'lib/prawn/table/cells.rb', line 55

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

#style(options = {}, &block) ⇒ Object

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 }


108
109
110
# File 'lib/prawn/table/cells.rb', line 108

def style(options={}, &block)
  each { |cell| cell.style(options, &block) }
end

#widthObject

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



114
115
116
117
118
119
120
121
# File 'lib/prawn/table/cells.rb', line 114

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