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", allocator: nil) ⇒ HeaderFooterBuilder

Returns a new instance of HeaderFooterBuilder.



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

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

Instance Attribute Details

#allocatorObject (readonly)

Returns the value of attribute allocator.



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

def allocator
  @allocator
end

#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)


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 34

def <<(element)
  case element
  when String
    para = ParagraphBuilder.new(allocator: @allocator)
    para << element
    @model.paragraphs << para.build
  when Wordprocessingml::Run
    if @model.paragraphs.empty?
      para = ParagraphBuilder.new(allocator: @allocator)
      para << element
      @model.paragraphs << para.build
    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(allocator: @allocator)
  para << text if text
  yield(para) if block
  @model.paragraphs << para.build
  para
end