Class: Caracal::Core::Models::TableModel

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/caracal/core/models/table_model.rb

Overview

This class handles block options passed to the table method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ TableModel

initialization



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/caracal/core/models/table_model.rb', line 45

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_header_rows    = DEFAULT_TABLE_HEADER_ROWS
  @table_rows_cant_split = {}

  super options, &block
end

Instance Attribute Details

#table_alignObject (readonly)

accessors



29
30
31
# File 'lib/caracal/core/models/table_model.rb', line 29

def table_align
  @table_align
end

#table_border_bottomObject (readonly)

returns border model



36
37
38
# File 'lib/caracal/core/models/table_model.rb', line 36

def table_border_bottom
  @table_border_bottom
end

#table_border_colorObject (readonly)

Returns the value of attribute table_border_color.



31
32
33
# File 'lib/caracal/core/models/table_model.rb', line 31

def table_border_color
  @table_border_color
end

#table_border_horizontalObject (readonly)

returns border model



39
40
41
# File 'lib/caracal/core/models/table_model.rb', line 39

def table_border_horizontal
  @table_border_horizontal
end

#table_border_leftObject (readonly)

returns border model



37
38
39
# File 'lib/caracal/core/models/table_model.rb', line 37

def table_border_left
  @table_border_left
end

#table_border_lineObject (readonly)

Returns the value of attribute table_border_line.



32
33
34
# File 'lib/caracal/core/models/table_model.rb', line 32

def table_border_line
  @table_border_line
end

#table_border_rightObject (readonly)

returns border model



38
39
40
# File 'lib/caracal/core/models/table_model.rb', line 38

def table_border_right
  @table_border_right
end

#table_border_sizeObject (readonly)

Returns the value of attribute table_border_size.



33
34
35
# File 'lib/caracal/core/models/table_model.rb', line 33

def table_border_size
  @table_border_size
end

#table_border_spacingObject (readonly)

Returns the value of attribute table_border_spacing.



34
35
36
# File 'lib/caracal/core/models/table_model.rb', line 34

def table_border_spacing
  @table_border_spacing
end

#table_border_topObject (readonly)

returns border model



35
36
37
# File 'lib/caracal/core/models/table_model.rb', line 35

def table_border_top
  @table_border_top
end

#table_border_verticalObject (readonly)

returns border model



40
41
42
# File 'lib/caracal/core/models/table_model.rb', line 40

def table_border_vertical
  @table_border_vertical
end

#table_column_widthsObject (readonly)

Returns the value of attribute table_column_widths.



42
43
44
# File 'lib/caracal/core/models/table_model.rb', line 42

def table_column_widths
  @table_column_widths
end

#table_header_rowsObject (readonly)

Returns the value of attribute table_header_rows.



41
42
43
# File 'lib/caracal/core/models/table_model.rb', line 41

def table_header_rows
  @table_header_rows
end

#table_widthObject (readonly)

Returns the value of attribute table_width.



30
31
32
# File 'lib/caracal/core/models/table_model.rb', line 30

def table_width
  @table_width
end

Instance Method Details

#calculate_width(container_width) ⇒ Object

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



88
89
90
91
92
# File 'lib/caracal/core/models/table_model.rb', line 88

def calculate_width(container_width)
  width(container_width) unless table_width.to_i > 0
  
  cells.each { |c| c.calculate_width(default_cell_width) }
end

#cant_split(models, options = {}) ⇒ Object

This method allows a user to specify one or more rows that should be rendered with the <w:cant_split /> property

For example

docx.table data do |t| t.cant_split r.rows end

would cause all rows in the table to be rendered with the <w:cant_split /> property.



121
122
123
124
125
126
# File 'lib/caracal/core/models/table_model.rb', line 121

def cant_split(models, options={})
  models = rows.include?(models) ? [models] : models
  models.each do |r|
    @table_rows_cant_split[rows.index(r)] = true
  end
end

#cant_split?(row_index) ⇒ Boolean

This method returns true or false depending on if a row has been marked as a can't split row via the cant_split method.

Returns:

  • (Boolean)


131
132
133
# File 'lib/caracal/core/models/table_model.rb', line 131

def cant_split?(row_index)
  !!@table_rows_cant_split[row_index]
end

#cell_style(models, options = {}) ⇒ Object

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, background: '3366cc', color: 'ffffff', bold: true end



103
104
105
106
107
# File 'lib/caracal/core/models/table_model.rb', line 103

def cell_style(models, options={})
  [models].flatten.compact.each do |m|
    m.apply_styles(options)
  end  
end

#cellsObject

DATA ACCESSORS =======================


64
65
66
# File 'lib/caracal/core/models/table_model.rb', line 64

def cells
  rows.flatten
end

#colsObject



68
69
70
71
72
73
74
75
76
# File 'lib/caracal/core/models/table_model.rb', line 68

def 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) ⇒ Object

column widths



184
185
186
# File 'lib/caracal/core/models/table_model.rb', line 184

def column_widths(value)
  @table_column_widths = value.map(&:to_i) if value.is_a?(Array)
end

#data(value) ⇒ Object

.data



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/caracal/core/models/table_model.rb', line 189

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

#rowsObject



78
79
80
# File 'lib/caracal/core/models/table_model.rb', line 78

def rows
  @table_data || [[]]
end

#valid?Boolean

VALIDATION ==============================

Returns:

  • (Boolean)


213
214
215
# File 'lib/caracal/core/models/table_model.rb', line 213

def valid?
  cells.first.is_a?(Caracal::Core::Models::TableCellModel)
end