Array
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'
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 69
69: def columns(col_spec)
70: index_cells unless @indexed
71: col_spec = transform_spec(col_spec, @column_count)
72: Cells.new(@columns[col_spec] ||= select{ |c| col_spec === c.column })
73: end
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 81
81: def filter(&block)
82: Cells.new(select(&block))
83: end
Returns the total height of all rows in the selected set.
# File lib/prawn/table/cells.rb, line 147
147: def height
148: row_heights = {}
149: each do |cell|
150: row_heights[cell.row] =
151: [row_heights[cell.row], cell.height].compact.max
152: end
153: row_heights.values.inject(0) { |sum, width| sum + width }
154: end
Returns maximum width that can contain cells in the set.
# File lib/prawn/table/cells.rb, line 136
136: def max_width
137: column_max_widths = {}
138: each do |cell|
139: column_max_widths[cell.column] =
140: [column_max_widths[cell.column], cell.max_width].compact.min
141: end
142: column_max_widths.values.inject(0) { |sum, width| sum + width }
143: end
Supports setting arbitrary properties on a group of cells.
table.cells.row(3..6).background_color = 'cc0000'
# File lib/prawn/table/cells.rb, line 160
160: def method_missing(id, *args, &block)
161: each { |c| c.send(id, *args, &block) }
162: end
Returns minimum width required to contain cells in the set.
# File lib/prawn/table/cells.rb, line 125
125: def min_width
126: column_min_widths = {}
127: each do |cell|
128: column_min_widths[cell.column] =
129: [column_min_widths[cell.column], cell.min_width].compact.max
130: end
131: column_min_widths.values.inject(0) { |sum, width| sum + width }
132: end
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 55
55: def rows(row_spec)
56: index_cells unless @indexed
57: row_spec = transform_spec(row_spec, @row_count)
58: Cells.new(@rows[row_spec] ||= select{ |c| row_spec === c.row })
59: end
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 108
108: def style(options={}, &block)
109: each { |cell| cell.style(options, &block) }
110: end
Returns the total width of all columns in the selected set.
# File lib/prawn/table/cells.rb, line 114
114: def width
115: column_widths = {}
116: each do |cell|
117: column_widths[cell.column] =
118: [column_widths[cell.column], cell.width].compact.max
119: end
120: column_widths.values.inject(0) { |sum, width| sum + width }
121: end
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 172
172: def index_cells
173: @rows = {}
174: @columns = {}
175:
176: each do |cell|
177: @rows[cell.row] ||= []
178: @rows[cell.row] << cell
179:
180: @columns[cell.column] ||= []
181: @columns[cell.column] << cell
182: end
183:
184: @row_count = @rows.size
185: @column_count = @columns.size
186:
187: @indexed = true
188: end
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 194
194: def transform_spec(spec, total)
195: case spec
196: when Range
197: transform_spec(spec.begin, total)..transform_spec(spec.end, total)
198: when Integer
199: spec < 0 ? (total + spec) : spec
200: else # pass through
201: spec
202: end
203: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.