Class: Uniword::Builder::TableCellBuilder

Inherits:
BaseBuilder
  • Object
show all
Includes:
HasBorders, HasShading
Defined in:
lib/uniword/builder/table_cell_builder.rb

Overview

Builds and configures TableCell objects.

Examples:

Create a table cell

r.cell do |c|
  c << 'Cell content'
  c.shading(fill: '4472C4')
end

Instance Attribute Summary

Attributes inherited from BaseBuilder

#model

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasShading

#shading

Methods included from HasBorders

#borders

Methods inherited from BaseBuilder

#build, from_model, #initialize

Constructor Details

This class inherits a constructor from Uniword::Builder::BaseBuilder

Class Method Details

.default_model_classObject



16
17
18
# File 'lib/uniword/builder/table_cell_builder.rb', line 16

def self.default_model_class
  Wordprocessingml::TableCell
end

Instance Method Details

#<<(element) ⇒ self

Append content to the cell. Routes by type:

  • String -> creates a Paragraph with a Run

  • Paragraph -> appends to paragraphs

  • Table -> appends to nested tables

Parameters:

  • element (String, Paragraph, Table)

Returns:

  • (self)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/uniword/builder/table_cell_builder.rb', line 27

def <<(element)
  case element
  when String
    para = Wordprocessingml::Paragraph.new
    para.runs << Wordprocessingml::Run.new(text: element)
    @model.paragraphs << para
  when Wordprocessingml::Paragraph
    @model.paragraphs << element
  when Wordprocessingml::Table
    @model.tables << element
  when ParagraphBuilder
    @model.paragraphs << element.build
  else
    raise ArgumentError, "Cannot add #{element.class} to table cell"
  end
  self
end

#column_span(count) ⇒ self

Set column span

Parameters:

  • count (Integer)

    Number of columns to span

Returns:

  • (self)


68
69
70
71
72
73
# File 'lib/uniword/builder/table_cell_builder.rb', line 68

def column_span(count)
  ensure_properties
  @model.properties.grid_span =
    Wordprocessingml::ValInt.new(value: count)
  self
end

#row_span(count) ⇒ self

Set row span

Parameters:

  • count (Integer)

    Number of rows to span

Returns:

  • (self)


79
80
81
82
83
84
# File 'lib/uniword/builder/table_cell_builder.rb', line 79

def row_span(count)
  ensure_properties
  @model.properties.v_merge =
    Wordprocessingml::ValInt.new(value: count)
  self
end

#vertical_align(value) ⇒ Object



58
59
60
61
62
# File 'lib/uniword/builder/table_cell_builder.rb', line 58

def vertical_align(value)
  ensure_properties
  @model.properties.vertical_align = Properties::CellVerticalAlign.new(value: value.to_s)
  self
end

#width(value, rule: nil) ⇒ self

Set cell width

Parameters:

  • value (Integer)

    Width in twips

  • rule (String) (defaults to: nil)

    Width rule (‘auto’, ‘exact’, ‘pct’)

Returns:

  • (self)


50
51
52
53
54
55
56
# File 'lib/uniword/builder/table_cell_builder.rb', line 50

def width(value, rule: nil)
  ensure_properties
  @model.properties.width ||= Properties::CellWidth.new
  @model.properties.width.value = value
  @model.properties.width.rule = rule if rule
  self
end