Class: BerkeleyLibrary::Util::ODS::XML::Table::Table

Inherits:
ElementNode
  • Object
show all
Defined in:
lib/berkeley_library/util/ods/xml/table/table.rb

Constant Summary collapse

MIN_COLUMNS =

Constants

1024
MIN_ROWS =
1_048_576

Instance Attribute Summary collapse

Attributes inherited from ElementNode

#doc, #element_name, #namespace

Instance Method Summary collapse

Methods inherited from ElementNode

#attributes, #clear_attribute, #element, #ensure_element!, #prefix, #prefixed_attr_name, #set_attribute

Constructor Details

#initialize(table_name, table_style = nil, styles:, protected: true) ⇒ Table

Initializes a new table

Parameters:



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 38

def initialize(table_name, table_style = nil, styles:, protected: true)
  super(:table, 'table', doc: styles.doc)

  @table_name = table_name
  @table_style = table_style || styles.default_style(:table)
  @styles = styles

  set_attribute('name', self.table_name)
  set_attribute('style-name', self.table_style.style_name)

  protect! if protected
end

Instance Attribute Details

#column_countObject


Accessors and utility methods



54
55
56
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 54

def column_count
  @column_count ||= 0
end

#row_countObject



58
59
60
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 58

def row_count
  @row_count ||= 0
end

#stylesXML::Office::AutomaticStyles (readonly)

Returns the document styles.

Returns:



27
28
29
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 27

def styles
  @styles
end

#table_nameObject (readonly)


Accessors



22
23
24
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 22

def table_name
  @table_name
end

#table_styleObject (readonly)

Returns the value of attribute table_style.



24
25
26
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 24

def table_style
  @table_style
end

Instance Method Details

#add_child(child) ⇒ Object


Public XML::ElementNode overrides



108
109
110
111
112
113
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 108

def add_child(child)
  return child.tap { |column| columns << column } if child.is_a?(TableColumn)
  return child.tap { |row| rows << row } if child.is_a?(TableRow)

  child.tap { |c| other_children << c }
end

#add_column(header, width = nil, protected: false) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 66

def add_column(header, width = nil, protected: false)
  add_column_with_styles(
    header,
    column_style: styles.find_or_create_column_style(width),
    default_cell_style: styles.find_or_create_cell_style(protected)
  )
end

#add_column_with_styles(header, column_style:, default_cell_style: nil, header_cell_style: nil) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 74

def add_column_with_styles(header, column_style:, default_cell_style: nil, header_cell_style: nil)
  cell_style = default_cell_style || styles.find_or_create_cell_style
  add_or_repeat_column(column_style, cell_style).tap do
    header_row = rows[0] || add_row
    header_row.set_value_at(column_count, header, header_cell_style)
    self.column_count += 1
  end
end

#add_empty_columns(number_repeated, width = nil, protected: false) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 83

def add_empty_columns(number_repeated, width = nil, protected: false)
  column_style = styles.find_or_create_column_style(width)
  default_cell_style = styles.find_or_create_cell_style(protected)

  TableColumn.new(column_style, default_cell_style, number_repeated, table: self).tap do |col|
    columns << col
    self.column_count += number_repeated
  end
end

#add_row(height = nil, number_repeated = 1) ⇒ TableRow

Adds a new row with the specified height.

Parameters:

  • height (String) (defaults to: nil)

    the row height. Defaults to Style::RowStyle::DEFAULT_HEIGHT.

  • number_repeated (Integer) (defaults to: 1)

    the number of identical rows to repeat

Returns:



97
98
99
100
101
102
103
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 97

def add_row(height = nil, number_repeated = 1)
  row_style = styles.find_or_create_row_style(height)
  TableRow.new(row_style, number_repeated, table: self).tap do |row|
    rows << row
    self.row_count += number_repeated
  end
end

#childrenObject (protected)


Protected XML::ElementNode overrides



123
124
125
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 123

def children
  [other_children, columns, rows].flatten
end

#create_elementObject (protected)



127
128
129
130
131
132
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 127

def create_element
  ensure_empty_columns!
  ensure_empty_rows!

  super
end

#get_value_at(row_index, column_index) ⇒ Object



62
63
64
# File 'lib/berkeley_library/util/ods/xml/table/table.rb', line 62

def get_value_at(row_index, column_index)
  (row = rows[row_index]) && row.get_value_at(column_index)
end