Class: Uniword::Docx::HeaderFooterPartCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/uniword/docx/header_footer_part_collection.rb

Overview

Ordered collection of HeaderFooterPart objects — the single storage path for all header/footer parts of a document, whether loaded from an existing DOCX or added programmatically.

The +headers+/+footers+ views (HeaderFooterView) and the serializer both read from this store, so a part exists exactly once: one part file, one relationship, one content-type override, one sectPr reference.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeaderFooterPartCollection

Returns a new instance of HeaderFooterPartCollection.



16
17
18
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 16

def initialize
  @parts = []
end

Class Method Details

.wrap(value) ⇒ HeaderFooterPart

Normalize one entry into a HeaderFooterPart. Legacy hashes describe loaded parts (they carry original rIds/targets).

Parameters:

Returns:



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 120

def self.wrap(value)
  return value if value.is_a?(HeaderFooterPart)

  HeaderFooterPart.new(
    r_id: value[:r_id],
    target: value[:target],
    rel_type: value[:rel_type],
    content_type: value[:content_type],
    type: value[:type],
    content: value[:content],
    loaded: true,
  )
end

Instance Method Details

#<<(part) ⇒ HeaderFooterPart Also known as: add

Append a part.

Parameters:

Returns:



46
47
48
49
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 46

def <<(part)
  @parts << part
  part
end

#delete(part) ⇒ HeaderFooterPart?

Remove a part (by identity).

Parameters:

Returns:



57
58
59
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 57

def delete(part)
  @parts.delete(part)
end

#each(&block) ⇒ Object

Iterate over all parts (both kinds) in insertion order.



21
22
23
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 21

def each(&block)
  @parts.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 33

def empty?
  @parts.empty?
end

#find_part(kind, type) ⇒ HeaderFooterPart?

Parameters:

  • kind (Symbol)

    :header or :footer

  • type (String, nil)

    sectPr reference type

Returns:



70
71
72
73
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 70

def find_part(kind, type)
  type = type&.to_s
  @parts.find { |part| part.kind == kind && part.type == type }
end

#next_target(kind) ⇒ String

First unused numbered target for a kind ("header3.xml" when header1/header2 exist).

Parameters:

  • kind (Symbol)

    :header or :footer

Returns:

  • (String)


87
88
89
90
91
92
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 87

def next_target(kind)
  numbers = of_kind(kind).filter_map do |part|
    part.target&.[](/\A#{kind}(\d+)\.xml\z/, 1)&.to_i
  end
  "#{kind}#{(numbers.max || 0) + 1}.xml"
end

#of_kind(kind) ⇒ Array<HeaderFooterPart>

Returns parts of that kind, in order.

Parameters:

  • kind (Symbol)

    :header or :footer

Returns:



63
64
65
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 63

def of_kind(kind)
  @parts.select { |part| part.kind == kind }
end

#replace_all(parts) ⇒ void

This method returns an undefined value.

Replace the entire store (legacy header_footer_parts= assignment). Accepts HeaderFooterPart objects and legacy part hashes ({ r_id:, target:, rel_type:, content_type:, content: }).

Parameters:



101
102
103
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 101

def replace_all(parts)
  @parts = Array(parts).map { |part| self.class.wrap(part) }
end

#replace_kind(kind, parts) ⇒ void

This method returns an undefined value.

Replace all parts of one kind, leaving the other untouched.

Parameters:



110
111
112
113
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 110

def replace_kind(kind, parts)
  @parts.reject! { |part| part.kind == kind }
  parts.each { |part| @parts << part }
end

#sizeInteger Also known as: length

Returns:

  • (Integer)


26
27
28
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 26

def size
  @parts.size
end

#targetsArray<String>

All non-nil relationship targets in the store.

Returns:

  • (Array<String>)


78
79
80
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 78

def targets
  @parts.filter_map(&:target)
end

#to_aArray<HeaderFooterPart>

Returns:



38
39
40
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 38

def to_a
  @parts.dup
end