Class: Uniword::Docx::HeaderFooterPartCollection
- Inherits:
-
Object
- Object
- Uniword::Docx::HeaderFooterPartCollection
- 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
-
.wrap(value) ⇒ HeaderFooterPart
Normalize one entry into a HeaderFooterPart.
Instance Method Summary collapse
-
#<<(part) ⇒ HeaderFooterPart
(also: #add)
Append a part.
-
#delete(part) ⇒ HeaderFooterPart?
Remove a part (by identity).
-
#each(&block) ⇒ Object
Iterate over all parts (both kinds) in insertion order.
- #empty? ⇒ Boolean
- #find_part(kind, type) ⇒ HeaderFooterPart?
-
#initialize ⇒ HeaderFooterPartCollection
constructor
A new instance of HeaderFooterPartCollection.
-
#next_target(kind) ⇒ String
First unused numbered target for a kind ("header3.xml" when header1/header2 exist).
-
#of_kind(kind) ⇒ Array<HeaderFooterPart>
Parts of that kind, in order.
-
#replace_all(parts) ⇒ void
Replace the entire store (legacy
header_footer_parts=assignment). -
#replace_kind(kind, parts) ⇒ void
Replace all parts of one kind, leaving the other untouched.
- #size ⇒ Integer (also: #length)
-
#targets ⇒ Array<String>
All non-nil relationship targets in the store.
- #to_a ⇒ Array<HeaderFooterPart>
Constructor Details
#initialize ⇒ HeaderFooterPartCollection
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).
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.
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).
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
33 34 35 |
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 33 def empty? @parts.empty? end |
#find_part(kind, type) ⇒ HeaderFooterPart?
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).
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.
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: }).
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.
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 |
#size ⇒ Integer Also known as: length
26 27 28 |
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 26 def size @parts.size end |
#targets ⇒ Array<String>
All non-nil relationship targets in the store.
78 79 80 |
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 78 def targets @parts.filter_map(&:target) end |
#to_a ⇒ Array<HeaderFooterPart>
38 39 40 |
# File 'lib/uniword/docx/header_footer_part_collection.rb', line 38 def to_a @parts.dup end |