Class: Prawn::Table::CellProxy
- Inherits:
-
Object
- Object
- Prawn::Table::CellProxy
- Includes:
- Enumerable
- Defined in:
- lib/prawn/table/accessors.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
-
#[](row, col) ⇒ Object
Retrieves a cell based on its 0-based row and column.
-
#columns(col_spec) ⇒ Object
(also: #column)
Limits selection to the given column or columns.
-
#each(&b) ⇒ Object
Iterates over cells in turn.
-
#height ⇒ Object
Returns the total height of all rows in the selected set.
-
#initialize(cells) ⇒ CellProxy
constructor
:nodoc:.
-
#max_width ⇒ Object
Returns maximum width that can contain cells in the set.
-
#method_missing(id, *args, &block) ⇒ Object
Supports setting arbitrary properties on a group of cells.
-
#min_width ⇒ Object
Returns minimum width required to contain cells in the set.
-
#rows(row_spec) ⇒ Object
(also: #row)
Limits selection to the given row or rows.
-
#select(&b) ⇒ Object
Selects cells based on a block.
-
#style(options = {}, &block) ⇒ Object
Supports setting multiple properties at once.
-
#width ⇒ Object
Returns the total width of all columns in the selected set.
Constructor Details
#initialize(cells) ⇒ CellProxy
:nodoc:
48 49 50 |
# File 'lib/prawn/table/accessors.rb', line 48 def initialize(cells) #:nodoc: @cells = cells end |
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'
171 172 173 |
# File 'lib/prawn/table/accessors.rb', line 171 def method_missing(id, *args, &block) @cells.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 a Cell, not a CellProxy.
table.cells[0, 0].content # => "First cell content"
98 99 100 |
# File 'lib/prawn/table/accessors.rb', line 98 def [](row, col) @cells.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
79 80 81 |
# File 'lib/prawn/table/accessors.rb', line 79 def columns(col_spec) CellProxy.new(@cells.select { |c| col_spec === c.column }) end |
#each(&b) ⇒ Object
Iterates over cells in turn.
54 55 56 |
# File 'lib/prawn/table/accessors.rb', line 54 def each(&b) @cells.each(&b) end |
#height ⇒ Object
Returns the total height of all rows in the selected set.
158 159 160 161 162 163 164 165 |
# File 'lib/prawn/table/accessors.rb', line 158 def height row_heights = {} @cells.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 ⇒ Object
Returns maximum width that can contain cells in the set.
147 148 149 150 151 152 153 154 |
# File 'lib/prawn/table/accessors.rb', line 147 def max_width column_max_widths = {} @cells.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_width ⇒ Object
Returns minimum width required to contain cells in the set.
136 137 138 139 140 141 142 143 |
# File 'lib/prawn/table/accessors.rb', line 136 def min_width column_min_widths = {} @cells.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
67 68 69 |
# File 'lib/prawn/table/accessors.rb', line 67 def rows(row_spec) CellProxy.new(@cells.select { |c| row_spec === c.row }) end |
#select(&b) ⇒ Object
Selects cells based on a block.
table.column(4).select { |cell| cell.content =~ /Yes/ }.
background_color = 'ff0000'
89 90 91 |
# File 'lib/prawn/table/accessors.rb', line 89 def select(&b) CellProxy.new(@cells.select(&b)) 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 }
116 117 118 119 120 121 |
# File 'lib/prawn/table/accessors.rb', line 116 def style(={}, &block) @cells.each do |cell| .each { |k, v| cell.send("#{k}=", v) } block.call(cell) if block end end |
#width ⇒ Object
Returns the total width of all columns in the selected set.
125 126 127 128 129 130 131 132 |
# File 'lib/prawn/table/accessors.rb', line 125 def width column_widths = {} @cells.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 |