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.



480
481
482
483
484
# File 'lib/hexapdf/document/layout.rb', line 480

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.



477
478
479
# File 'lib/hexapdf/document/layout.rb', line 477

def argument_infos
  @argument_infos
end

Instance Method Details

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

Stores the keyword arguments in args for the given 0-based rows and columns which can either be a single number or a range of numbers.



488
489
490
491
492
# File 'lib/hexapdf/document/layout.rb', line 488

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.



498
499
500
501
502
503
504
505
506
# File 'lib/hexapdf/document/layout.rb', line 498

def retrieve_arguments_for(row, col)
  @argument_infos.each_with_object({}) do |arg_info, result|
    next unless arg_info.rows.cover?(row) && arg_info.cols.cover?(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