Class: Uniword::HeadersFooters::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/headers_footers/manager.rb

Overview

Manages headers and footers for a document.

Provides CRUD operations for headers and footers with support for default, first-page, and even-page types.

Examples:

Add a header with text

manager = Manager.new(doc)
manager.add_header("Confidential", type: "default")

Add page numbers to footer

manager.add_footer("Page {PAGE}", type: "default")

Constant Summary collapse

TYPES =
%w[default first even].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Manager

Returns a new instance of Manager.



21
22
23
# File 'lib/uniword/headers_footers/manager.rb', line 21

def initialize(document)
  @document = document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



19
20
21
# File 'lib/uniword/headers_footers/manager.rb', line 19

def document
  @document
end

Instance Method Details

Add a footer to the document.

Parameters:

  • text (String)

    Footer text content

  • type (String) (defaults to: "default")

    Footer type (default/first/even)

Returns:

Raises:

  • (ArgumentError)

    If type is invalid



76
77
78
79
80
81
82
83
84
85
# File 'lib/uniword/headers_footers/manager.rb', line 76

def add_footer(text, type: "default")
  validate_type(type)

  footer = Uniword::Footer.new(type: type)
  add_text_to(footer, text)

  document.footers ||= []
  document.footers << footer
  footer
end

#add_header(text, type: "default") ⇒ Uniword::Header

Add a header to the document.

Parameters:

  • text (String)

    Header text content

  • type (String) (defaults to: "default")

    Header type (default/first/even)

Returns:

Raises:

  • (ArgumentError)

    If type is invalid



59
60
61
62
63
64
65
66
67
68
# File 'lib/uniword/headers_footers/manager.rb', line 59

def add_header(text, type: "default")
  validate_type(type)

  header = Uniword::Header.new(type: type)
  add_text_to(header, text)

  document.headers ||= []
  document.headers << header
  header
end

#clear_allvoid

This method returns an undefined value.

Clear all headers and footers.



114
115
116
117
# File 'lib/uniword/headers_footers/manager.rb', line 114

def clear_all
  document.headers = []
  document.footers = []
end

#list_footersArray<Hash>

List all footers in the document.

Returns:

  • (Array<Hash>)

    Each hash has :type, :text, :empty keys



42
43
44
45
46
47
48
49
50
51
# File 'lib/uniword/headers_footers/manager.rb', line 42

def list_footers
  footers = document.footers || []
  footers.map do |f|
    {
      type: f.type,
      text: extract_text(f),
      empty: f.empty?,
    }
  end
end

#list_headersArray<Hash>

List all headers in the document.

Returns:

  • (Array<Hash>)

    Each hash has :type, :text, :empty keys



28
29
30
31
32
33
34
35
36
37
# File 'lib/uniword/headers_footers/manager.rb', line 28

def list_headers
  headers = document.headers || []
  headers.map do |h|
    {
      type: h.type,
      text: extract_text(h),
      empty: h.empty?,
    }
  end
end

#remove_footers(type:) ⇒ Integer

Remove footers by type.

Parameters:

  • type (String)

    Footer type to remove (default/first/even)

Returns:

  • (Integer)

    Number of footers removed



103
104
105
106
107
108
109
# File 'lib/uniword/headers_footers/manager.rb', line 103

def remove_footers(type:)
  return 0 unless document.footers

  before = document.footers.size
  document.footers = document.footers.reject { |f| f.type == type }
  before - document.footers.size
end

#remove_headers(type:) ⇒ Integer

Remove headers by type.

Parameters:

  • type (String)

    Header type to remove (default/first/even)

Returns:

  • (Integer)

    Number of headers removed



91
92
93
94
95
96
97
# File 'lib/uniword/headers_footers/manager.rb', line 91

def remove_headers(type:)
  return 0 unless document.headers

  before = document.headers.size
  document.headers = document.headers.reject { |h| h.type == type }
  before - document.headers.size
end