Class: Uniword::Builder::HeaderFooterBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/builder/header_footer_builder.rb

Overview

Builds and configures Header or Footer objects.

Examples:

Create a header

doc.header do |h|
  h << 'Document Title'
end

Create a footer with page number

doc.footer do |f|
  f << 'Page '
  f << Builder.page_number_field
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, type: "default") ⇒ HeaderFooterBuilder

Returns a new instance of HeaderFooterBuilder.



20
21
22
23
24
25
26
27
# File 'lib/uniword/builder/header_footer_builder.rb', line 20

def initialize(kind, type: "default")
  @kind = kind
  @model = if kind == :header
             Wordprocessingml::Header.new
           else
             Wordprocessingml::Footer.new
           end
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



18
19
20
# File 'lib/uniword/builder/header_footer_builder.rb', line 18

def kind
  @kind
end

#modelObject (readonly)

Returns the value of attribute model.



18
19
20
# File 'lib/uniword/builder/header_footer_builder.rb', line 18

def model
  @model
end

Instance Method Details

#<<(element) ⇒ self

Append content to the header/footer

Parameters:

Returns:

  • (self)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/uniword/builder/header_footer_builder.rb', line 33

def <<(element)
  case element
  when String
    para = Wordprocessingml::Paragraph.new
    para.runs << Wordprocessingml::Run.new(text: element)
    @model.paragraphs << para
  when Wordprocessingml::Run
    # Auto-wrap Run in a paragraph, appending to the last one if possible
    if @model.paragraphs.empty?
      para = Wordprocessingml::Paragraph.new
      para.runs << element
      @model.paragraphs << para
    else
      @model.paragraphs.last.runs << element
    end
  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 #{@kind}"
  end
  self
end

#buildObject

Return the underlying Header or Footer model



74
75
76
# File 'lib/uniword/builder/header_footer_builder.rb', line 74

def build
  @model
end

#paragraph(text = nil) {|ParagraphBuilder| ... } ⇒ ParagraphBuilder

Add a paragraph with configuration

Parameters:

  • text (String, nil) (defaults to: nil)

    Optional text content

Yields:

Returns:



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

def paragraph(text = nil, &block)
  para = ParagraphBuilder.new
  para << text if text
  yield(para) if block
  @model.paragraphs << para.build
  para
end