Class: Uniword::Docx::HeaderFooterView

Inherits:
Object
  • Object
show all
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 sectPr type and delegate paragraphs, tables and empty? 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

Instance Method Summary collapse

Constructor Details

#initialize(store, kind) ⇒ HeaderFooterView

Returns a new instance of HeaderFooterView.

Parameters:



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

#kindSymbol (readonly)

Returns :header or :footer.

Returns:

  • (Symbol)

    :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.

Parameters:

  • model (Object)

    content model or HeaderFooterPart

Returns:

  • (Object)

    the appended model



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.

Parameters:

  • type (String, Symbol)

    "default"/"first"/"even"

Returns:



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.

Parameters:

  • type (String, Symbol)

    sectPr reference type

  • model (Object)

    Wordprocessingml::Header/Footer or legacy Uniword::Header/Footer

Returns:

  • (Object)

    the assigned model



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.

Parameters:

  • type (String, Symbol)

Returns:

  • (Object, nil)

    the removed part's content



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

Returns:

  • (Boolean)


133
134
135
# File 'lib/uniword/docx/header_footer_view.rb', line 133

def empty?
  size.zero?
end

#key?(type) ⇒ Boolean

Parameters:

  • type (String, Symbol)

Returns:

  • (Boolean)


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

def key?(type)
  !@store.find_part(kind, type).nil?
end

#keysArray<String, nil>

Returns sectPr types.

Returns:

  • (Array<String, nil>)

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

Parameters:

Returns:

  • (Object)

    the assigned value



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

#sizeInteger

Returns:

  • (Integer)


128
129
130
# File 'lib/uniword/docx/header_footer_view.rb', line 128

def size
  @store.of_kind(kind).size
end

#valuesArray

Returns content models.

Returns:

  • (Array)

    content models



105
106
107
# File 'lib/uniword/docx/header_footer_view.rb', line 105

def values
  map(&:content)
end