class SvgDrawer::Table

Public Class Methods

col_widths(col_widths, width, columns) click to toggle source

Infer col_widths from width (if needed)

# File lib/svg_drawer/table/table.rb, line 13
def self.col_widths(col_widths, width, columns)
  return if col_widths.nil? && width.nil?
  sum_width = col_widths.reduce(&:+) if col_widths

  if col_widths && width && sum_width != width
    raise ArgumentError, "Sum of given col widths (#{col_widths}) doesn't match total element width (#{width})"
  end

  col_widths || Array.new(columns, width.to_d / columns)
end

Public Instance Methods

add_row(row) click to toggle source
# File lib/svg_drawer/table/table.rb, line 46
def add_row(row)
  raise TypeError, "Expected Row, got: #{row.class}" unless row.is_a?(Row)
  row.update_params!(inherited: row_params)
  rows << row
end
blank_row(params = {}) click to toggle source
# File lib/svg_drawer/table/table.rb, line 100
def blank_row(params = {})
  raise ArgumentError, ':height required' if !param(:row_height) && !params[:height]
  rows << BlankRow.new(params.merge(inherited: row_params))
  self
end
circle_row(center, radius, params = {}) click to toggle source
# File lib/svg_drawer/table/table.rb, line 87
def circle_row(center, radius, params = {})
  row(params) { |r| r.circle_cell(center, radius) }
end
col_widths() click to toggle source
# File lib/svg_drawer/table/table.rb, line 106
def col_widths
  Table.col_widths(param(:col_widths), param(:width), param(:columns))
end
height() click to toggle source
# File lib/svg_drawer/table/table.rb, line 32
def height
  ensure_complete!
  sum_height = rows.reduce(0) { |a, e| a + e.height }
  [param(:height, 0), sum_height].max
end
image_row(href, params = {}) click to toggle source
# File lib/svg_drawer/table/table.rb, line 91
def image_row(href, params = {})
  row(params) { |r| r.image_cell(href) }
end
incomplete() click to toggle source
# File lib/svg_drawer/table/table.rb, line 38
def incomplete
  rows.none? ? self : find_incomplete_descendant
end
line_row(points, params = {}) click to toggle source
# File lib/svg_drawer/table/table.rb, line 75
def line_row(points, params = {})
  row(params) { |r| r.line_cell(points) }
end
multipolyline_row(strokes, params = {}) click to toggle source
# File lib/svg_drawer/table/table.rb, line 83
def multipolyline_row(strokes, params = {})
  row(params) { |r| r.multipolyline_cell(strokes) }
end
path_row(path_components, params = {}) click to toggle source
# File lib/svg_drawer/table/table.rb, line 71
def path_row(path_components, params = {})
  row(params) { |r| r.path_cell(path_components) }
end
polyline_row(points, params = {}) click to toggle source
# File lib/svg_drawer/table/table.rb, line 79
def polyline_row(points, params = {})
  row(params) { |r| r.polyline_cell(points) }
end
row(params = {}) { |row| ... } click to toggle source

The params hash can contain a special :height value It will be used instead of the @row_height when creating the row

@param params [Hash] row params @return [Table] self

# File lib/svg_drawer/table/table.rb, line 59
def row(params = {})
  row = Row.new(params.merge(inherited: row_params))
  yield(row)
  rows << row
  self
end
rows() click to toggle source
# File lib/svg_drawer/table/table.rb, line 42
def rows
  @rows ||= []
end
sub_table_row(params = {}) { |t| ... } click to toggle source
# File lib/svg_drawer/table/table.rb, line 95
def sub_table_row(params = {})
  t = Table.new(params)
  row { |r| r.cell { |c| c.content(t) && yield(t) } }
end
text_row(texts, params = {}) click to toggle source
# File lib/svg_drawer/table/table.rb, line 66
def text_row(texts, params = {})
  texts = texts.nil? ? [nil] : Array(texts)
  row(params) { |r| texts.each { |text| r.text_cell(text) } }
end
width() click to toggle source
# File lib/svg_drawer/table/table.rb, line 24
def width
  ensure_complete!
  sum_width = col_widths ? col_widths.reduce(&:+) : 0
  max_width = max_col_widths.reduce(&:+)

  [sum_width, max_width].max
end

Private Instance Methods

_draw(parent) click to toggle source

The dimension overrides given when the table is actually the child of a (parent) table cell. In this case the overrides are used to draw proper borders (since the Cell element of the parent determines its)

@param parent [Rasem::SVGTagWithParent] @param width_override [Integer] (optional) container width @param height_override [Integer] (optional) container height @return [Rasem::SVGTagWithParent]

# File lib/svg_drawer/table/table.rb, line 123
def _draw(parent)
  Utils::RasemWrapper.group(parent, class: param(:class), id: param(:id)) do |table_group|
    draw_border(table_group)

    rows.reduce(0) do |y, row|
      row.draw(table_group, max_col_widths, debug: @debug).translate(0, y)
      y + row.height
    end
  end
end
find_incomplete_descendant() click to toggle source
# File lib/svg_drawer/table/table.rb, line 140
def find_incomplete_descendant
  rows.each.with_object(nil) do |row, _|
    res = row.incomplete
    break res if res
  end
end
max_col_widths() click to toggle source
# File lib/svg_drawer/table/table.rb, line 147
def max_col_widths
  rows.map(&:cell_widths).transpose.map(&:max)
end
row_params() click to toggle source
# File lib/svg_drawer/table/table.rb, line 134
def row_params
  param(:row_height)
  return child_params unless param(:row_height)
  child_params.merge(height: param(:row_height))
end