Class: HexaPDF::Document::Layout::CellArgumentCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/document/layout.rb

Overview

This helper class is used by Layout#table_box to allow specifying the keyword arguments used when converting cell data to box instances.

Defined Under Namespace

Classes: ArgumentInfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number_of_rows, number_of_columns) ⇒ CellArgumentCollector

Creates a new instance, providing the number of rows and columns of the table.



497
498
499
500
501
# File 'lib/hexapdf/document/layout.rb', line 497

def initialize(number_of_rows, number_of_columns)
  @argument_infos = []
  @number_of_rows = number_of_rows
  @number_of_columns = number_of_columns
end

Instance Attribute Details

#argument_infosObject (readonly)

Returns all stored ArgumentInfo instances.



494
495
496
# File 'lib/hexapdf/document/layout.rb', line 494

def argument_infos
  @argument_infos
end

Instance Method Details

#[]=(rows = 0..-1,, cols = 0..-1,, args) ⇒ Object

Stores the hash args containing styling properties for the cells specified via the given 0-based rows and columns.

Rows and columns can either be single numbers, ranges of numbers or stepped ranges (i.e. Enumerator::ArithmeticSequence instances).

Examples:

# Gray background for all cells
args[] = {cell: {background_color: "gray"}}

# Cell at (2, 3) gets a bigger font size
args[2, 3] = {font_size: 50}

# First column of every row has bold font
args[0..-1, 0] = {font: 'Helvetica bold'}

# Every second row has a blue background
args[(0..-1).step(2)] = {cell: {background_color: "blue"}}


522
523
524
525
526
# File 'lib/hexapdf/document/layout.rb', line 522

def []=(rows = 0..-1, cols = 0..-1, args)
  rows = adjust_range(rows.kind_of?(Integer) ? rows..rows : rows, @number_of_rows)
  cols = adjust_range(cols.kind_of?(Integer) ? cols..cols : cols, @number_of_columns)
  @argument_infos << ArgumentInfo.new(rows, cols, args)
end

#retrieve_arguments_for(row, col) ⇒ Object

Retrieves the merged keyword arguments for the cell in row and col.

Earlier defined arguments are overridden by later ones, except for the :cell key which is merged.



532
533
534
535
536
537
538
539
540
# File 'lib/hexapdf/document/layout.rb', line 532

def retrieve_arguments_for(row, col)
  @argument_infos.each_with_object({}) do |arg_info, result|
    next unless arg_info.rows.include?(row) && arg_info.cols.include?(col)
    if arg_info.args[:cell]
      arg_info.args[:cell] = (result[:cell] || {}).merge(arg_info.args[:cell])
    end
    result.update(arg_info.args)
  end
end