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



41
42
43
44
45
46
47
48
49
# File 'lib/caracal/core/models/table_model.rb', line 41

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
  
  super options, &block
end

Instance Attribute Details

#table_alignObject (readonly)

accessors



27
28
29
# File 'lib/caracal/core/models/table_model.rb', line 27

def table_align
  @table_align
end

#table_border_bottomObject (readonly)

returns border model



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

def table_border_bottom
  @table_border_bottom
end

#table_border_colorObject (readonly)

Returns the value of attribute table_border_color.



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

def table_border_color
  @table_border_color
end

#table_border_horizontalObject (readonly)

returns border model



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

def table_border_horizontal
  @table_border_horizontal
end

#table_border_leftObject (readonly)

returns border model



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

def table_border_left
  @table_border_left
end

#table_border_lineObject (readonly)

Returns the value of attribute table_border_line.



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

def table_border_line
  @table_border_line
end

#table_border_rightObject (readonly)

returns border model



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

def table_border_right
  @table_border_right
end

#table_border_sizeObject (readonly)

Returns the value of attribute table_border_size.



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

def table_border_size
  @table_border_size
end

#table_border_spacingObject (readonly)

Returns the value of attribute table_border_spacing.



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

def table_border_spacing
  @table_border_spacing
end

#table_border_topObject (readonly)

returns border model



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

def table_border_top
  @table_border_top
end

#table_border_verticalObject (readonly)

returns border model



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

def table_border_vertical
  @table_border_vertical
end

#table_widthObject (readonly)

Returns the value of attribute table_width.



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

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.



82
83
84
85
86
# File 'lib/caracal/core/models/table_model.rb', line 82

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 = {}) ⇒ 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[0], background: '3366cc', color: 'ffffff', bold: true

end



97
98
99
100
101
# File 'lib/caracal/core/models/table_model.rb', line 97

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

#cellsObject

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


58
59
60
# File 'lib/caracal/core/models/table_model.rb', line 58

def cells
  rows.flatten
end

#colsObject



62
63
64
65
66
67
68
69
70
# File 'lib/caracal/core/models/table_model.rb', line 62

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

#data(value) ⇒ Object

.data



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/caracal/core/models/table_model.rb', line 153

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



72
73
74
# File 'lib/caracal/core/models/table_model.rb', line 72

def rows
  @table_data || [[]]
end

#valid?Boolean

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

Returns:

  • (Boolean)


177
178
179
# File 'lib/caracal/core/models/table_model.rb', line 177

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