Class: Uniword::Section

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/uniword/section.rb

Overview

Represents a document section Each section can have its own page setup, headers, and footers

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Section

Returns a new instance of Section.



13
14
15
16
17
18
# File 'lib/uniword/section.rb', line 13

def initialize(**attributes)
  super
  @properties ||= SectionProperties.a4_portrait
  @headers ||= []
  @footers ||= []
end

Instance Method Details

Add a footer to this section



31
32
33
# File 'lib/uniword/section.rb', line 31

def add_footer(footer)
  footers << footer
end

#add_header(header) ⇒ Object

Add a header to this section



21
22
23
24
25
26
27
28
# File 'lib/uniword/section.rb', line 21

def add_header(header)
  unless header.is_a?(Header)
    raise ArgumentError,
          "header must be a Header instance"
  end

  headers << header
end

Convenience accessors for different footer types



96
97
98
# File 'lib/uniword/section.rb', line 96

def default_footer
  get_footer("default")
end

#default_footer=(footer) ⇒ Object



100
101
102
103
104
# File 'lib/uniword/section.rb', line 100

def default_footer=(footer)
  footers.reject! { |f| f.type == "default" }
  footer.type = "default" if footer
  add_footer(footer) if footer
end

#default_headerObject

Convenience accessors for different header types



64
65
66
# File 'lib/uniword/section.rb', line 64

def default_header
  get_header("default")
end

#default_header=(header) ⇒ Object



68
69
70
71
72
73
# File 'lib/uniword/section.rb', line 68

def default_header=(header)
  # Remove existing default header
  headers.reject! { |h| h.type == "default" }
  header.type = "default" if header
  add_header(header) if header
end


116
117
118
# File 'lib/uniword/section.rb', line 116

def even_footer
  get_footer("even")
end

#even_footer=(footer) ⇒ Object



120
121
122
123
124
# File 'lib/uniword/section.rb', line 120

def even_footer=(footer)
  footers.reject! { |f| f.type == "even" }
  footer.type = "even" if footer
  add_footer(footer) if footer
end

#even_headerObject



85
86
87
# File 'lib/uniword/section.rb', line 85

def even_header
  get_header("even")
end

#even_header=(header) ⇒ Object



89
90
91
92
93
# File 'lib/uniword/section.rb', line 89

def even_header=(header)
  headers.reject! { |h| h.type == "even" }
  header.type = "even" if header
  add_header(header) if header
end


106
107
108
# File 'lib/uniword/section.rb', line 106

def first_footer
  get_footer("first")
end

#first_footer=(footer) ⇒ Object



110
111
112
113
114
# File 'lib/uniword/section.rb', line 110

def first_footer=(footer)
  footers.reject! { |f| f.type == "first" }
  footer.type = "first" if footer
  add_footer(footer) if footer
end

#first_headerObject



75
76
77
# File 'lib/uniword/section.rb', line 75

def first_header
  get_header("first")
end

#first_header=(header) ⇒ Object



79
80
81
82
83
# File 'lib/uniword/section.rb', line 79

def first_header=(header)
  headers.reject! { |h| h.type == "first" }
  header.type = "first" if header
  add_header(header) if header
end

Get or create footer by type



55
56
57
58
59
60
61
# File 'lib/uniword/section.rb', line 55

def footer(type = "default")
  get_footer(type) || begin
    f = Footer.new(type: type)
    add_footer(f)
    f
  end
end

Get footer by type



41
42
43
# File 'lib/uniword/section.rb', line 41

def get_footer(type = "default")
  footers.find { |f| f.type == type }
end

#get_header(type = "default") ⇒ Object

Get header by type



36
37
38
# File 'lib/uniword/section.rb', line 36

def get_header(type = "default")
  headers.find { |h| h.type == type }
end

#has_footers?Boolean

Check if section has any footers

Returns:

  • (Boolean)


132
133
134
# File 'lib/uniword/section.rb', line 132

def has_footers?
  footers.any?
end

#has_headers?Boolean

Check if section has any headers

Returns:

  • (Boolean)


127
128
129
# File 'lib/uniword/section.rb', line 127

def has_headers?
  headers.any?
end

#header(type = "default") ⇒ Object

Get or create header by type



46
47
48
49
50
51
52
# File 'lib/uniword/section.rb', line 46

def header(type = "default")
  get_header(type) || begin
    h = Header.new(type: type)
    add_header(h)
    h
  end
end

#page_marginsHash

Get page margins as hash (docx-js compatibility) Returns hash with margin values in twips

Returns:

  • (Hash)

    Margin configuration



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/uniword/section.rb', line 140

def page_margins
  ensure_properties
  {
    top: @properties.margin_top,
    bottom: @properties.margin_bottom,
    left: @properties.margin_left,
    right: @properties.margin_right,
    header: @properties.margin_header,
    footer: @properties.margin_footer,
  }
end

#page_margins=(value) ⇒ Hash

Set page margins from hash (docx-js compatibility) Accepts hash with margin values in twips

Parameters:

  • value (Hash)

    Margin configuration

Options Hash (value):

  • :top (Integer)

    Top margin in twips

  • :bottom (Integer)

    Bottom margin in twips

  • :left (Integer)

    Left margin in twips

  • :right (Integer)

    Right margin in twips

  • :header (Integer)

    Header margin in twips

  • :footer (Integer)

    Footer margin in twips

Returns:

  • (Hash)

    the value set



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/uniword/section.rb', line 163

def page_margins=(value)
  return unless value.is_a?(Hash)

  ensure_properties
  @properties.margin_top = value[:top] if value.key?(:top)
  @properties.margin_bottom = value[:bottom] if value.key?(:bottom)
  @properties.margin_left = value[:left] if value.key?(:left)
  @properties.margin_right = value[:right] if value.key?(:right)
  @properties.margin_header = value[:header] if value.key?(:header)
  @properties.margin_footer = value[:footer] if value.key?(:footer)
  value
end