Class: BarkestCore::PdfTableBuilder
- Inherits:
-
Object
- Object
- BarkestCore::PdfTableBuilder
- Defined in:
- app/models/barkest_core/pdf_table_builder.rb
Overview
A table builder to ease creation of tables in PDF documents.
Instance Method Summary collapse
-
#base_cell_options ⇒ Object
Gets the cell options provided during the TableBuilder creation.
-
#bold_cell(options = {}, &block) ⇒ Object
Creates a bold cell.
-
#bold_italic_cell(options = {}, &block) ⇒ Object
Creates a bold-italic cell.
-
#build_column(start_column = nil) ⇒ Object
Builds data starting at the specified column.
-
#cell(options = {}, &block) ⇒ Object
Generates a cell in the current row.
-
#cells(options = {}, &block) ⇒ Object
Creates multiple cells.
-
#current_column ⇒ Object
Gets the current column in the table.
-
#current_row ⇒ Object
Gets the current row in the table.
-
#initialize(document, options = {}) {|_self| ... } ⇒ PdfTableBuilder
constructor
Creates a new table builder in the provided
document
. -
#italic_cell(options = {}, &block) ⇒ Object
Creates an italicized cell.
-
#key_value(options = {}, &block) ⇒ Object
Creates a pair of cells.
-
#last_column ⇒ Object
Gets the last column in the table.
-
#last_row ⇒ Object
Gets the last row in the table.
-
#position ⇒ Object
Gets the current position in the table.
-
#row(options = {}, &block) ⇒ Object
Builds a row in the table.
-
#subtable(cell_options = {}, options = {}, &block) ⇒ Object
Builds a subtable within the current row.
-
#table_options ⇒ Object
Gets the table options provided during the TableBuilder creation.
-
#to_table ⇒ Object
Generates the table array used by Prawn::Table.
-
#underline_cell(options = {}, &block) ⇒ Object
Creates an underlined cell.
Constructor Details
#initialize(document, options = {}) {|_self| ... } ⇒ PdfTableBuilder
Creates a new table builder in the provided document
.
The options can specify both table options and :cell_style options. The :cell_style option should be a hash of styles for the cells in your table.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 14 def initialize(document, = {}, &block) ||= {} @doc = document @data = [] @col_offset = 0 @row_offset = 0 @col_start = 0 @in_row = false @base_cell_options = { borders: [] }.merge([:cell_style] || { }) @options = .except(:cell_style) yield self if block_given? end |
Instance Method Details
#base_cell_options ⇒ Object
Gets the cell options provided during the TableBuilder creation.
33 34 35 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 33 def @base_cell_options end |
#bold_cell(options = {}, &block) ⇒ Object
Creates a bold cell.
See #cell for valid options.
165 166 167 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 165 def bold_cell( = {}, &block) cell({ font_style: :bold }.merge( || {}), &block) end |
#bold_italic_cell(options = {}, &block) ⇒ Object
Creates a bold-italic cell.
See #cell for valid options.
183 184 185 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 183 def bold_italic_cell( = {}, &block) cell({ font_style: :bold_italic }.merge( || {}), &block) end |
#build_column(start_column = nil) ⇒ Object
Builds data starting at the specified column.
The start_column
is the first column you want to be building. When you start a new row inside of a build_column block, the new row starts at this same column.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 89 def build_column(start_column = nil) if block_given? raise StandardError.new('build_column block called within row block') if @in_row raise StandardError.new('build_column called without valid argument') unless start_column.is_a?(Numeric) backup_col_start = @col_start backup_col_offset = @col_offset backup_row_offset = @row_offset @col_start = start_column.to_i @col_offset = @col_start @row_offset = 0 yield @col_start = backup_col_start @col_offset = backup_col_offset @row_offset = backup_row_offset end @col_start end |
#cell(options = {}, &block) ⇒ Object
Generates a cell in the current row.
Valid options:
- value
-
The value to put in the cell, unless a code block is provided, in which case the result of the code block is used.
- rowspan
-
The number of rows for this cell to cover.
- colspan
-
The number of columns for this cell to cover.
Additional options are embedded and passed on to Prawn::Table, see Prawn PDF Table Manual for more information.
275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 275 def cell( = {}, &block) raise StandardError.new('cell called outside of row block') unless @in_row = @row_cell_options.merge( || {}) = change_col() result = block_given? ? yield : ([:value] || '') .except!(:value) set_cell(result, nil, nil, ) end |
#cells(options = {}, &block) ⇒ Object
Creates multiple cells.
Individual cells can be given options by prefixing the keys with ‘cell_#’ where # is the cell number (starting at 1).
See #cell for valid options.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 203 def cells( = {}, &block) cell_regex = /^cell_([0-9]+)_/ ||= { } result = block_given? ? yield : ([:values] || ['']) = result.map { {} } = {} .each do |k,v| # if the option starts with 'cell_#_' then apply it accordingly. if (m = cell_regex.match(k.to_s)) k = k.to_s[m[0].length..-1].to_sym [m[1].to_i - 1][k] = v # the 'column' option applies only to the first cell. elsif k == :column [0][k] = v # everything else applies to all cells, unless overridden explicitly. elsif k != :values [k] = v end end .each_with_index do |opt,idx| cell .merge(opt).merge( { value: result[idx] } ) end end |
#current_column ⇒ Object
Gets the current column in the table.
63 64 65 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 63 def current_column @col_offset end |
#current_row ⇒ Object
Gets the current row in the table.
57 58 59 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 57 def current_row @row_offset end |
#italic_cell(options = {}, &block) ⇒ Object
Creates an italicized cell.
See #cell for valid options.
174 175 176 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 174 def italic_cell( = {}, &block) cell({ font_style: :italic }.merge( || {}), &block) end |
#key_value(options = {}, &block) ⇒ Object
Creates a pair of cells. The first cell is bold with the :key option and the second cell is normal with the :value option.
Additional options can be specified as they are in #cells.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 240 def key_value( = {}, &block) ||= {} if [:key] [:values] ||= [] [:values][0] = [:key] end if [:value] [:values] ||= [] [:values][1] = [:value] end = { cell_1_font_style: :bold }.merge(.except(:key, :value)) cells , &block end |
#last_column ⇒ Object
Gets the last column in the table.
75 76 77 78 79 80 81 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 75 def last_column max = 0 @data.each do |row| max = row.length if max < row.length end max - 1 end |
#last_row ⇒ Object
Gets the last row in the table.
69 70 71 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 69 def last_row @data.length - 1 end |
#position ⇒ Object
Gets the current position in the table.
51 52 53 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 51 def position [ @row_offset, @col_offset ] end |
#row(options = {}, &block) ⇒ Object
Builds a row in the table.
Valid options:
- row
-
Defines the row you want to start on. If not set, then it uses #current_row.
Additional options are merged with the base cell options for this row.
When it completes, the #current_row is set to 1 more than the row we started on.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 122 def row( = {}, &block) raise StandardError.new('row called within row block') if @in_row @in_row = true @col_offset = @col_start = change_row( || {}) @row_cell_options = @base_cell_options.merge() fill_cells(@row_offset, @col_offset) # skip placeholders when starting a new row. if @data[@row_offset] while @data[@row_offset][@col_offset] == :span_placeholder @col_offset += 1 end end yield if block_given? @in_row = false @row_offset += 1 @row_cell_options = nil end |
#subtable(cell_options = {}, options = {}, &block) ⇒ Object
Builds a subtable within the current row.
The cell_options
are passed to the current cell. The options
are passed to the new TableBuilder.
153 154 155 156 157 158 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 153 def subtable( = {}, = {}, &block) raise StandardError.new('subtable called outside of row block') unless @in_row cell || {} do PdfTableBuilder.new(@doc, || {}, &block).to_table end end |
#table_options ⇒ Object
Gets the table options provided during the TableBuilder creation.
39 40 41 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 39 def @options end |
#to_table ⇒ Object
Generates the table array used by Prawn::Table.
45 46 47 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 45 def to_table @doc.make_table(data, ) end |
#underline_cell(options = {}, &block) ⇒ Object
Creates an underlined cell.
See #cell for valid options.
192 193 194 |
# File 'app/models/barkest_core/pdf_table_builder.rb', line 192 def underline_cell( = {}, &block) cell({ borders: [ :bottom ], border_width: 0.5 }.merge( || {}), &block) end |