class Caracal::Core::Models::TableModel

This class handles block options passed to the table method.

Attributes

table_align[R]

accessors

table_border_bottom[R]
table_border_color[R]
table_border_horizontal[R]
table_border_left[R]
table_border_line[R]
table_border_right[R]
table_border_size[R]
table_border_spacing[R]
table_border_top[R]
table_border_vertical[R]
table_column_widths[R]
table_repeat_header[R]
table_width[R]

Public Class Methods

new(options={}, &block) click to toggle source

initialization

Calls superclass method Caracal::Core::Models::BaseModel::new
# File lib/caracal/core/models/table_model.rb, line 44
def initialize(options={}, &block)
  @table_align          = DEFAULT_TABLE_ALIGN
  @table_border_color   = DEFAULT_TABLE_BORDER_COLOR
  @table_border_line    = DEFAULT_TABLE_BORDER_LINE
  @table_border_size    = DEFAULT_TABLE_BORDER_SIZE
  @table_border_spacing = DEFAULT_TABLE_BORDER_SPACING
  @table_repeat_header  = DEFAULT_TABLE_REPEAT_HEADER
  
  super options, &block
end

Public Instance Methods

calculate_width(container_width) click to toggle source

This method sets explicit widths on all wrapped cells that do not already have widths asided.

# File lib/caracal/core/models/table_model.rb, line 86
def calculate_width(container_width)
  width(container_width) unless table_width.to_i > 0
  
  cells.each { |c| c.calculate_width(default_cell_width) }
end
cell_style(models, options={}) click to toggle source

This method allows tables to be styled several cells at a time.

For example, this would style a header row.

docx.table data do |t|

t.cell_style t.rows[0], background: '3366cc', color: 'ffffff', bold: true

end

# File lib/caracal/core/models/table_model.rb, line 101
def cell_style(models, options={})
  [models].flatten.compact.each do |m|
    m.apply_styles(options)
  end  
end
cells() click to toggle source
DATA ACCESSORS =======================
# File lib/caracal/core/models/table_model.rb, line 62
def cells
  rows.flatten
end
cols() click to toggle source
# File lib/caracal/core/models/table_model.rb, line 66
def cols
  @cols ||= rows.reduce([]) do |array, row|
    row.each_with_index do |cell, index|
      array[index]  = []    if array[index].nil?
      array[index] << cell
    end
    array
  end
end
column_widths(value) click to toggle source

column widths

# File lib/caracal/core/models/table_model.rb, line 157
def column_widths(value)
  @table_column_widths = value.map(&:to_i) if value.is_a?(Array)
end
data(value) click to toggle source

.data

# File lib/caracal/core/models/table_model.rb, line 162
def data(value)
  begin
    @table_data = value.map do |data_row|
      data_row.map do |data_cell|
        case data_cell
        when Caracal::Core::Models::TableCellModel
          data_cell
        when Hash
          Caracal::Core::Models::TableCellModel.new(data_cell)
        when Proc
          Caracal::Core::Models::TableCellModel.new(&data_cell)
        else
          Caracal::Core::Models::TableCellModel.new({ content: data_cell.to_s })
        end
      end
    end
  rescue
    raise Caracal::Errors::InvalidTableDataError, 'Table data must be a two-dimensional array.'
  end
end
rows() click to toggle source
# File lib/caracal/core/models/table_model.rb, line 76
def rows
  @table_data || [[]]
end
valid?() click to toggle source
VALIDATION ==============================
# File lib/caracal/core/models/table_model.rb, line 186
def valid?
  cells.first.is_a?(Caracal::Core::Models::TableCellModel)
end

Private Instance Methods

default_cell_width() click to toggle source
# File lib/caracal/core/models/table_model.rb, line 196
def default_cell_width
  cell_widths     = rows.first.map { |c| c.cell_width.to_i }
  remaining_width = table_width - cell_widths.reduce(&:+).to_i
  remaining_cols  = cols.size - cell_widths.reject { |w| w == 0 }.size
  default_width   = (remaining_cols == 0) ? 0 : (remaining_width / remaining_cols)
end
option_keys() click to toggle source
# File lib/caracal/core/models/table_model.rb, line 203
def option_keys
  k = []
  k << [:data, :align, :width]
  k << [:border_color, :border_line, :border_size, :border_spacing]
  k << [:border_bottom, :border_left, :border_right, :border_top, :border_horizontal, :border_vertical]
  k << [:column_widths]
  k << [:repeat_header]
  k.flatten
end