class BerkeleyLibrary::Util::ODS::XML::Table::TableRow

Attributes

default_cell_style[R]
row_style[R]

Accessors

Public Class Methods

new(row_style, number_repeated = 1, table:, default_cell_style: nil) click to toggle source

@param table [Table] the table @param default_cell_style [Style::CellStyle] the default cell style

# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 24
def initialize(row_style, number_repeated = 1, table:, default_cell_style: nil)
  super('table-row', 'number-rows-repeated', number_repeated, table: table)
  @row_style = row_style
  @default_cell_style = default_cell_style

  set_default_attributes!
end

Public Instance Methods

add_child(child) click to toggle source

Public XML::ElementNode overrides

# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 46
def add_child(child)
  return add_table_cell(child) if child.is_a?(TableCell)

  child.tap { |c| other_children << c }
end
get_value_at(column_index) click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 39
def get_value_at(column_index)
  (cell = explicit_cells[column_index]) && cell.value
end
set_value_at(column_index, value = nil, cell_style = nil) click to toggle source

Public utility methods

# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 35
def set_value_at(column_index, value = nil, cell_style = nil)
  explicit_cells[column_index] = TableCell.new(value, cell_style || default_cell_style, table: table)
end

Protected Instance Methods

add_table_cell(cell) click to toggle source

Protected utility methods

# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 60
def add_table_cell(cell)
  return cell.tap { |c| explicit_cells << c } if explicit_cell_count < table.column_count

  raise ArgumentError, "Can't add cell at column index #{explicit_cell_count} to table with only #{table.column_count} columns"
end
children() click to toggle source

Protected XML::ElementNode overrides

# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 69
def children
  [].tap do |cc|
    each_cell { |c| cc << c }
    cc.concat(other_children)
  end
end

Private Instance Methods

column_count_actual() click to toggle source

Private methods

# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 81
def column_count_actual
  [table.column_count, Table::MIN_COLUMNS].max
end
each_cell(columns_yielded = 0, remaining = explicit_cells, &block) click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 101
def each_cell(columns_yielded = 0, remaining = explicit_cells, &block)
  columns_yielded, remaining = yield_while_non_nil(columns_yielded, remaining, &block)
  columns_yielded, remaining = yield_while_nil(columns_yielded, remaining, &block)
  each_cell(columns_yielded, remaining, &block) unless remaining.empty?
end
explicit_cell_count() click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 97
def explicit_cell_count
  explicit_cells.size
end
explicit_cells() click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 85
def explicit_cells
  @explicit_cells ||= []
end
other_children() click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 93
def other_children
  @other_children ||= []
end
set_default_attributes!() click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 89
def set_default_attributes!
  set_attribute('style-name', row_style.style_name)
end
yield_repeat_empty(num_repeats, &block) click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 126
def yield_repeat_empty(num_repeats, &block)
  empty_cell = TableCell.repeat_empty(num_repeats, default_cell_style, table: table)
  block.call(empty_cell)
end
yield_while_nil(columns_yielded, remaining, &block) click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 115
def yield_while_nil(columns_yielded, remaining, &block)
  nil_cell_count = Arrays.count_while(values: remaining, &:nil?)
  remaining = remaining[nil_cell_count..]

  empty_required = remaining.empty? ? (column_count_actual - columns_yielded) : nil_cell_count
  yield_repeat_empty(empty_required, &block)
  columns_yielded += empty_required

  [columns_yielded, remaining]
end
yield_while_non_nil(columns_yielded, remaining, &block) click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table_row.rb, line 107
def yield_while_non_nil(columns_yielded, remaining, &block)
  non_nil_cells = remaining.take_while { |c| !c.nil? }
  non_nil_cells.each(&block)
  non_nil_cell_count = non_nil_cells.size

  [columns_yielded + non_nil_cell_count, remaining[non_nil_cell_count..]]
end