Class: Arrolio::Table::AutoLayout

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/table/auto_layout.rb

Overview

Computes column widths for a Content::Table based on natural content widths. Each column gets at least its natural width (longest unbreakable token + padding). Remaining space is distributed proportionally to the column's natural width, so wider columns get more extra space.

If the total natural width exceeds the available width, columns are scaled down proportionally (each gets a share proportional to its natural width, but never below a minimum).

Constant Summary collapse

MIN_COLUMN_WIDTH =

points — prevents columns from collapsing

20.0
CELL_PADDING =

points — left + right padding per cell

8.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, available_width:, measurer: nil) ⇒ AutoLayout

Returns a new instance of AutoLayout.



20
21
22
23
24
# File 'lib/arrolio/table/auto_layout.rb', line 20

def initialize(table, available_width:, measurer: nil)
  @table = table
  @available_width = available_width.to_f
  @measurer = measurer || default_measurer
end

Instance Attribute Details

#available_widthObject (readonly)

Returns the value of attribute available_width.



18
19
20
# File 'lib/arrolio/table/auto_layout.rb', line 18

def available_width
  @available_width
end

#measurerObject (readonly)

Returns the value of attribute measurer.



18
19
20
# File 'lib/arrolio/table/auto_layout.rb', line 18

def measurer
  @measurer
end

#tableObject (readonly)

Returns the value of attribute table.



18
19
20
# File 'lib/arrolio/table/auto_layout.rb', line 18

def table
  @table
end

Instance Method Details

#computeObject

Returns an Array of Float widths (one per column), summing to available_width.



28
29
30
31
32
33
# File 'lib/arrolio/table/auto_layout.rb', line 28

def compute
  return Array.new(column_count, @available_width / column_count) if column_count.zero?

  natural = natural_widths
  distribute(natural)
end