Class: BarkestCore::PdfTableBuilder

Inherits:
Object
  • Object
show all
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

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.

Yields:

  • (_self)

Yield Parameters:



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, options = {}, &block)
  options ||= {}

  @doc = document

  @data = []
  @col_offset = 0
  @row_offset = 0
  @col_start = 0
  @in_row = false

  @base_cell_options = { borders: [] }.merge(options[:cell_style] || { })
  @options = options.except(:cell_style)

  yield self if block_given?
end

Instance Method Details

#base_cell_optionsObject

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
  @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(options = {}, &block)
  cell({ font_style: :bold }.merge(options || {}), &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(options = {}, &block)
  cell({ font_style: :bold_italic }.merge(options || {}), &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.

Raises:

  • (StandardError)


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(options = {}, &block)
  raise StandardError.new('cell called outside of row block') unless @in_row

  options = @row_cell_options.merge(options || {})

  options = change_col(options)

  result = block_given? ? yield : (options[:value] || '')

  options.except!(:value)

  set_cell(result, nil, nil, options)
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(options = {}, &block)
  cell_regex = /^cell_([0-9]+)_/

  options ||= { }

  result = block_given? ? yield : (options[:values] || [''])

  cell_options = result.map { {} }
  common_options = {}

  options.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
      cell_options[m[1].to_i - 1][k] = v

    # the 'column' option applies only to the first cell.
    elsif k == :column
      cell_options[0][k] = v

    # everything else applies to all cells, unless overridden explicitly.
    elsif k != :values
      common_options[k] = v
    end
  end

  cell_options.each_with_index do |opt,idx|
    cell common_options.merge(opt).merge( { value: result[idx] } )
  end
end

#current_columnObject

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_rowObject

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(options = {}, &block)
  cell({ font_style: :italic }.merge(options || {}), &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(options = {}, &block)
  options ||= {}

  if options[:key]
    options[:values] ||= []
    options[:values][0] = options[:key]
  end
  if options[:value]
    options[:values] ||= []
    options[:values][1] = options[:value]
  end

  options = {
      cell_1_font_style: :bold
  }.merge(options.except(:key, :value))

  cells options, &block
end

#last_columnObject

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_rowObject

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

#positionObject

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.

Raises:

  • (StandardError)


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(options = {}, &block)
  raise StandardError.new('row called within row block') if @in_row

  @in_row = true
  @col_offset = @col_start
  options = change_row(options || {})

  @row_cell_options = @base_cell_options.merge(options)

  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.

Raises:

  • (StandardError)


153
154
155
156
157
158
# File 'app/models/barkest_core/pdf_table_builder.rb', line 153

def subtable(cell_options = {}, options = {}, &block)
  raise StandardError.new('subtable called outside of row block') unless @in_row
  cell cell_options || {} do
    PdfTableBuilder.new(@doc, options || {}, &block).to_table
  end
end

#table_optionsObject

Gets the table options provided during the TableBuilder creation.



39
40
41
# File 'app/models/barkest_core/pdf_table_builder.rb', line 39

def table_options
  @options
end

#to_tableObject

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, table_options)
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(options = {}, &block)
  cell({ borders: [ :bottom ], border_width: 0.5 }.merge(options || {}), &block)
end