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.



580
581
582
583
584
# File 'lib/hexapdf/document/layout.rb', line 580

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.



577
578
579
# File 'lib/hexapdf/document/layout.rb', line 577

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"}}


605
606
607
608
609
# File 'lib/hexapdf/document/layout.rb', line 605

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.



615
616
617
618
619
620
621
622
623
624
# File 'lib/hexapdf/document/layout.rb', line 615

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]
      result.update(arg_info.args, cell: (result[:cell] || {}).merge(arg_info.args[:cell]))
    else
      result.update(arg_info.args)
    end
  end
end