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

Constructor Details

#initialize(model = nil) ⇒ TableCellBuilder

Returns a new instance of TableCellBuilder.



20
21
22
23
# File 'lib/uniword/builder/table_cell_builder.rb', line 20

def initialize(model = nil)
  super
  ensure_cell_structure
end

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)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/uniword/builder/table_cell_builder.rb', line 32

def <<(element)
  case element
  when String
    para = Wordprocessingml::Paragraph.new
    para.runs << Wordprocessingml::Run.new(text: element)
    @model.paragraphs << para
    track_element_order("p")
  when Wordprocessingml::Paragraph
    @model.paragraphs << element
    track_element_order("p")
  when Wordprocessingml::Table
    @model.tables << element
    track_element_order("tbl")
  when ParagraphBuilder
    @model.paragraphs << element.build
    track_element_order("p")
  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)


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

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)


88
89
90
91
92
93
# File 'lib/uniword/builder/table_cell_builder.rb', line 88

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

#vertical_align(value) ⇒ Object



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

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)


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

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