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'

Experimental API collapse

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'


195
196
197
198
199
200
201
# File 'lib/prawn/table/cells.rb', line 195

def method_missing(id, *args, &block)
  if id.to_s =~ /=\z/
    each { |c| c.send(id, *args, &block) if c.respond_to?(id) }
  else
    super
  end
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"


109
110
111
112
113
114
115
116
117
118
# File 'lib/prawn/table/cells.rb', line 109

def [](row, col)
  return nil if empty?
  index_cells unless defined?(@indexed) && @indexed
  row_array, col_array = @rows[@first_row + row] || [], @columns[@first_column + col] || []
  if row_array.length < col_array.length
    row_array.find { |c| c.column == @first_column + col }
  else
    col_array.find { |c| c.row == @first_row + row }
  end
end

#[]=(row, col, cell) ⇒ Object

Puts a cell in the collection at the given position. Internal use only.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/prawn/table/cells.rb', line 122

def []=(row, col, cell) # :nodoc:
  cell.extend(Cell::InTable)
  cell.row = row
  cell.column = col

  if defined?(@indexed) && @indexed
    (@rows[row]    ||= []) << cell
    (@columns[col] ||= []) << cell
    @first_row    = row if !@first_row    || row < @first_row
    @first_column = col if !@first_column || col < @first_column
    @row_count    = @rows.size
    @column_count = @columns.size
  end

  self << cell
end

#column_countObject

Returns the number of columns in the list.



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

def column_count
  index_cells unless defined?(@indexed) && @indexed
  @column_count
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


79
80
81
82
83
84
85
# File 'lib/prawn/table/cells.rb', line 79

def columns(col_spec)
  index_cells unless defined?(@indexed) && @indexed
  col_spec = transform_spec(col_spec, @first_column, @column_count)
  Cells.new(@columns[col_spec] ||= select { |c|
              col_spec.respond_to?(:include?) ?
                col_spec.include?(c.column) : 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'


100
101
102
# File 'lib/prawn/table/cells.rb', line 100

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

#fits_on_current_page?(offset, ref_bounds) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/prawn/table/cells.rb', line 40

def fits_on_current_page?(offset, ref_bounds)
  # an empty row array means it definitely fits
  return true if self.empty?

  height_with_span < (self[0,0].y + offset) - ref_bounds.absolute_bottom
end

#heightObject

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



180
181
182
# File 'lib/prawn/table/cells.rb', line 180

def height
  aggregate_cell_values(:row, :height_ignoring_span, :max)
end

#height_with_spanObject

Returns the total height of all rows in the selected set including spanned cells if the cell is the master cell



187
188
189
# File 'lib/prawn/table/cells.rb', line 187

def height_with_span
  aggregate_cell_values(:row, :height, :max)
end

#max_widthObject

Returns maximum width that can contain cells in the set.



174
175
176
# File 'lib/prawn/table/cells.rb', line 174

def max_width
  aggregate_cell_values(:column, :max_width_ignoring_span, :max)
end

#min_widthObject

Returns minimum width required to contain cells in the set.



168
169
170
# File 'lib/prawn/table/cells.rb', line 168

def min_width
  aggregate_cell_values(:column, :avg_spanned_min_width, :max)
end

#row_countObject

Returns the number of rows in the list.



67
68
69
70
# File 'lib/prawn/table/cells.rb', line 67

def row_count
  index_cells unless defined?(@indexed) && @indexed
  @row_count
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


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

def rows(row_spec)
  index_cells unless defined?(@indexed) && @indexed
  row_spec = transform_spec(row_spec, @first_row, @row_count)
  Cells.new(@rows[row_spec] ||= select { |c|
              row_spec.respond_to?(:include?) ?
                row_spec.include?(c.row) : 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 }


153
154
155
156
157
158
# File 'lib/prawn/table/cells.rb', line 153

def style(options={}, &block)
  each do |cell|
    next if cell.is_a?(Cell::SpanDummy)
    cell.style(options, &block)
  end
end

#widthObject

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



162
163
164
# File 'lib/prawn/table/cells.rb', line 162

def width
  ColumnWidthCalculator.new(self).natural_widths.inject(0, &:+)
end