Class: Uniword::Docx::HeaderFooterView
- Inherits:
-
Object
- Object
- Uniword::Docx::HeaderFooterView
- Includes:
- Enumerable
- Defined in:
- lib/uniword/docx/header_footer_view.rb
Overview
Kind-filtered view over a document's HeaderFooterPartCollection.
document.headers and document.footers return these views, so
the Builder, the headers/footers managers and the serializer all
share the single underlying store.
Two historic access shapes are preserved:
- Hash-style (Builder):
view["default"] = header_model,view["default"],each_value,values,keys,key?,delete. Content access yields the Header/Footer models. - Array-style (managers):
view << header,each,map,find,reject. Iteration yields HeaderFooterPart objects, which carry the sectPrtypeand delegateparagraphs,tablesandempty?to their content.
Upsert semantics: assigning a type that already exists replaces that part's content in place (keeping its target and rId), so loaded and builder-created parts can never duplicate.
Instance Attribute Summary collapse
-
#kind ⇒ Symbol
readonly
:header or :footer.
Instance Method Summary collapse
-
#<<(model) ⇒ Object
Append a model (Array-style).
-
#[](type) ⇒ Wordprocessingml::Header, ...
Hash-style read: content model for a sectPr type.
-
#[]=(type, model) ⇒ Object
Hash-style write: upsert a content model by sectPr type.
-
#delete(type) ⇒ Object?
Remove the part for a sectPr type.
-
#each(&block) ⇒ Object
Iterate over the parts of this kind (Array-style access).
-
#each_key(&block) ⇒ Object
Iterate over sectPr types.
-
#each_value(&block) ⇒ Object
Iterate over content models.
- #empty? ⇒ Boolean
-
#initialize(store, kind) ⇒ HeaderFooterView
constructor
A new instance of HeaderFooterView.
- #key?(type) ⇒ Boolean
-
#keys ⇒ Array<String, nil>
SectPr types.
-
#replace(value) ⇒ Object
Bulk assignment (+document.headers = value+).
- #size ⇒ Integer
-
#values ⇒ Array
Content models.
Constructor Details
#initialize(store, kind) ⇒ HeaderFooterView
Returns a new instance of HeaderFooterView.
31 32 33 34 |
# File 'lib/uniword/docx/header_footer_view.rb', line 31 def initialize(store, kind) @store = store @kind = kind end |
Instance Attribute Details
#kind ⇒ Symbol (readonly)
Returns :header or :footer.
27 28 29 |
# File 'lib/uniword/docx/header_footer_view.rb', line 27 def kind @kind end |
Instance Method Details
#<<(model) ⇒ Object
Append a model (Array-style). Legacy Uniword::Header/Footer
models supply their own type; other models are stored
without a type.
80 81 82 83 84 85 86 87 |
# File 'lib/uniword/docx/header_footer_view.rb', line 80 def <<(model) if model.is_a?(HeaderFooterPart) adopt_part(model) else append_model(model) end model end |
#[](type) ⇒ Wordprocessingml::Header, ...
Hash-style read: content model for a sectPr type.
45 46 47 |
# File 'lib/uniword/docx/header_footer_view.rb', line 45 def [](type) @store.find_part(kind, type)&.content end |
#[]=(type, model) ⇒ Object
Hash-style write: upsert a content model by sectPr type. Replaces the existing part's content in place when the type exists; otherwise appends a new part with the next free numbered target.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/uniword/docx/header_footer_view.rb', line 58 def []=(type, model) type = type.to_s content = coerce_content(model) part = @store.find_part(kind, type) if part part.content = content part.loaded = false else @store << HeaderFooterPart.new( kind: kind, type: type, content: content, target: @store.next_target(kind) ) end model end |
#delete(type) ⇒ Object?
Remove the part for a sectPr type.
119 120 121 122 123 124 125 |
# File 'lib/uniword/docx/header_footer_view.rb', line 119 def delete(type) part = @store.find_part(kind, type) return nil unless part @store.delete(part) part.content end |
#each(&block) ⇒ Object
Iterate over the parts of this kind (Array-style access).
37 38 39 |
# File 'lib/uniword/docx/header_footer_view.rb', line 37 def each(&block) @store.of_kind(kind).each(&block) end |
#each_key(&block) ⇒ Object
Iterate over sectPr types.
90 91 92 |
# File 'lib/uniword/docx/header_footer_view.rb', line 90 def each_key(&block) map(&:type).each(&block) end |
#each_value(&block) ⇒ Object
Iterate over content models.
95 96 97 |
# File 'lib/uniword/docx/header_footer_view.rb', line 95 def each_value(&block) map(&:content).each(&block) end |
#empty? ⇒ Boolean
133 134 135 |
# File 'lib/uniword/docx/header_footer_view.rb', line 133 def empty? size.zero? end |
#key?(type) ⇒ Boolean
111 112 113 |
# File 'lib/uniword/docx/header_footer_view.rb', line 111 def key?(type) !@store.find_part(kind, type).nil? end |
#keys ⇒ Array<String, nil>
Returns sectPr types.
100 101 102 |
# File 'lib/uniword/docx/header_footer_view.rb', line 100 def keys map(&:type) end |
#replace(value) ⇒ Object
Bulk assignment (+document.headers = value+). Accepts nil (clears this kind), a Hash of type => model, an Array of models/parts, or the view itself (no-op).
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/uniword/docx/header_footer_view.rb', line 143 def replace(value) case value when nil then @store.replace_kind(kind, []) when HeaderFooterView then nil when Hash then replace_from_hash(value) when Array then replace_from_array(value) else raise ArgumentError, "cannot assign #{value.class} to document.#{kind}s" end value end |
#size ⇒ Integer
128 129 130 |
# File 'lib/uniword/docx/header_footer_view.rb', line 128 def size @store.of_kind(kind).size end |
#values ⇒ Array
Returns content models.
105 106 107 |
# File 'lib/uniword/docx/header_footer_view.rb', line 105 def values map(&:content) end |