class Prawn::Table::ColumnWidthCalculator

def aggregate_cell_values(row_or_column, meth, aggregate)

Parameters:
  • aggregate () -- - functions from cell.rb to be used to aggregate e.g. avg_spanned_min_width
  • meth () -- - min/max
  • row_or_column () -- - you may call this on either rows or columns
def aggregate_cell_values(row_or_column, meth, aggregate)
  values = {}
  #calculate values for all cells that do not span accross multiple cells
  #this ensures that we don't have a problem if the first line includes
  #a cell that spans across multiple cells
  @cells.each do |cell|
    #don't take spanned cells
    if cell.colspan == 1 and cell.class != Prawn::Table::Cell::SpanDummy
      index = cell.send(row_or_column)
      values[index] = [values[index], cell.send(meth)].compact.send(aggregate)
    end
  end
  # if there are only colspanned or rowspanned cells in a table
  spanned_width_needs_fixing = true
  @cells.each do |cell|
    index = cell.send(row_or_column)
    if cell.colspan > 1
      #special treatment if some but not all spanned indices in the values array have been calculated
      #only applies to rows
      values = fill_values_if_needed(values, cell, index, meth) if row_or_column == :column
      #calculate current (old) return value before we do anything
      old_sum = 0
      cell.colspan.times { |i|
        old_sum += values[index+i] unless values[index+i].nil?
      }
      #calculate future return value
      new_sum = cell.send(meth) * cell.colspan
      #due to float rounding errors we need to ignore a small difference in the new
      #and the old sum the same had to be done in
      #the column_width_calculator#natural_width
      spanned_width_needs_fixing = ((new_sum - old_sum) > Prawn::FLOAT_PRECISION)
      if spanned_width_needs_fixing
        #not entirely sure why we need this line, but with it the tests pass
        values[index] = [values[index], cell.send(meth)].compact.send(aggregate)
        #overwrite the old values with the new ones, but only if all entries existed
        entries_exist = true
        cell.colspan.times { |i| entries_exist = false if values[index+i].nil? }
        cell.colspan.times { |i|
          values[index+i] = cell.send(meth) if entries_exist
        }
      end
    else
      if spanned_width_needs_fixing && cell.class == Prawn::Table::Cell::SpanDummy
        values[index] = [values[index], cell.send(meth)].compact.send(aggregate)
      end
    end
  end
  return values.values.inject(0, &:+)
end