class BerkeleyLibrary::Util::ODS::XML::Office::AutomaticStyles

Public Class Methods

new(doc:) click to toggle source

Initializer

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 17
def initialize(doc:)
  super(:office, 'automatic-styles', doc: doc)
end

Public Instance Methods

add_cell_style(name = nil, protected = false, color = nil, font_weight: nil, wrap: false) click to toggle source

rubocop:disable Style/OptionalBooleanParameter

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 37
def add_cell_style(name = nil, protected = false, color = nil, font_weight: nil, wrap: false)
  name ||= next_name_for(:table_cell)
  add_style(Style::CellStyle.new(name, protected, color, font_weight: font_weight, wrap: wrap, styles: self))
end
add_child(child) click to toggle source

Public XML::ElementNode overrides

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 106
def add_child(child)
  child.is_a?(Style::Style) ? add_style(child) : child.tap { |c| other_children << c }
end
add_column_style(name = nil, width = nil) click to toggle source

rubocop:enable Style/OptionalBooleanParameter

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 43
def add_column_style(name = nil, width = nil)
  name ||= next_name_for(:table_column)
  add_style(Style::ColumnStyle.new(name, width, styles: self))
end
add_row_style(name = nil, height = nil) click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 48
def add_row_style(name = nil, height = nil)
  name ||= next_name_for(:table_row)
  add_style(Style::RowStyle.new(name, height, styles: self))
end
add_style(style) click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 58
def add_style(style)
  raise ArgumentError, "Not a style: #{style.inspect}" unless style.is_a?(Style::Style)

  style.tap { |s| add_or_insert_style(s) }
end
add_table_style(name = nil) click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 53
def add_table_style(name = nil)
  name ||= next_name_for(:table)
  add_style(Style::TableStyle.new(name, styles: self))
end
default_style(family) click to toggle source

Accessors

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 24
def default_style(family)
  first_style = styles_for_family(family).first
  return first_style if first_style

  add_default_style(family)
end
find_cell_style(protected = false, color: nil, font_weight: nil, wrap: false) click to toggle source

rubocop:disable Style/OptionalBooleanParameter

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 65
def find_cell_style(protected = false, color: nil, font_weight: nil, wrap: false)
  styles_for_family(:table_cell).find { |s| [s.protected?, s.color, s.font_weight, s.wrap?] == [protected, color, font_weight, wrap] }
end
find_column_style(width = nil) click to toggle source

rubocop:enable Style/OptionalBooleanParameter

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 70
def find_column_style(width = nil)
  w = width || Style::ColumnStyle::DEFAULT_WIDTH
  styles_for_family(:table_column).find { |s| s.width == w }
end
find_or_create_cell_style(protected = false, color: nil, font_weight: nil, wrap: false) click to toggle source

rubocop:disable Style/OptionalBooleanParameter

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 81
def find_or_create_cell_style(protected = false, color: nil, font_weight: nil, wrap: false)
  existing_style = find_cell_style(protected, color: color, font_weight: font_weight, wrap: wrap)
  return existing_style if existing_style

  add_cell_style(nil, protected, color, font_weight: font_weight, wrap: wrap)
end
find_or_create_column_style(width = nil) click to toggle source

rubocop:enable Style/OptionalBooleanParameter

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 89
def find_or_create_column_style(width = nil)
  existing_style = find_column_style(width)
  return existing_style if existing_style

  add_column_style(nil, width)
end
find_or_create_row_style(height = nil) click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 96
def find_or_create_row_style(height = nil)
  existing_style = find_row_style(height)
  return existing_style if existing_style

  add_row_style(nil, height)
end
find_row_style(height = nil) click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 75
def find_row_style(height = nil)
  h = height || Style::RowStyle::DEFAULT_HEIGHT
  styles_for_family(:table_row).find { |s| s.height == h }
end

Protected Instance Methods

children() click to toggle source

Protected XML::ElementNode overrides

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 118
def children
  [other_children, Style::Family.map { |f| styles_for_family(f) }].flatten
end
create_element() click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 122
def create_element
  Style::Family.each { |f| default_style(f) }

  super
end

Private Instance Methods

add_default_style(family) click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 143
def add_default_style(family)
  f = Style::Family.ensure_family(family)
  return add_cell_style if f == Style::Family::TABLE_CELL
  return add_column_style if f == Style::Family::TABLE_COLUMN
  return add_row_style if f == Style::Family::TABLE_ROW
  return add_table_style if f == Style::Family::TABLE
end
add_or_insert_style(s) click to toggle source

Private methods

# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 133
def add_or_insert_style(s)
  styles = styles_for_family(s.family)
  insert_index = styles.find_index do |s1|
    raise ArgumentError, "A #{s.family} style named #{s.style_name} already exists" if s1.style_name == s.style_name

    s1 > s
  end
  insert_index ? styles.insert(insert_index, s) : styles << s
end
next_name_for(family) click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 151
def next_name_for(family)
  f = Style::Family.ensure_family(family)

  max_suffix = styles_for_family(f).inject(0) do |max, s|
    next max unless (n = s.style_name).start_with?(f.prefix)
    next max unless (suffix = n[f.prefix.size..]) =~ /^[0-9]+$/

    [max, suffix.to_i].max
  end

  "#{f.prefix}#{max_suffix + 1}"
end
other_children() click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 172
def other_children
  @other_children ||= []
end
styles_by_family() click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 168
def styles_by_family
  @styles_by_family ||= {}
end
styles_for_family(family) click to toggle source
# File lib/berkeley_library/util/ods/xml/office/automatic_styles.rb, line 164
def styles_for_family(family)
  (styles_by_family[Style::Family.ensure_family(family)] ||= [])
end