Class: Uniword::Builder::SdtBuilder

Inherits:
BaseBuilder show all
Defined in:
lib/uniword/builder/sdt_builder.rb

Overview

Builds Structured Document Tags (content controls).

Supports text controls, date pickers, dropdown lists, bibliography placeholders, and document part references.

Examples:

Create a text content control

sdt = SdtBuilder.text(tag: 'title', alias: 'Document Title')
doc.paragraph { |p| p << sdt.build }

Create a date picker

sdt = SdtBuilder.date(tag: 'date1', format: 'yyyy-MM-dd')
doc.paragraph { |p| p << sdt.build }

Create a bibliography placeholder

sdt = SdtBuilder.bibliography
doc.paragraph { |p| p << sdt.build }

Instance Attribute Summary

Attributes inherited from BaseBuilder

#model

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseBuilder

from_model

Constructor Details

#initialize(model = nil) ⇒ SdtBuilder

Returns a new instance of SdtBuilder.



26
27
28
29
# File 'lib/uniword/builder/sdt_builder.rb', line 26

def initialize(model = nil)
  super
  @sdt_id_counter = 0
end

Class Method Details

.bibliographySdtBuilder

Create a bibliography placeholder

Returns:



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/uniword/builder/sdt_builder.rb', line 148

def self.bibliography
  sdt = new

  sdt.properties.bibliography = Wordprocessingml::StructuredDocumentTag::Bibliography.new

  dpo = Wordprocessingml::StructuredDocumentTag::DocPartObj.new
  dpo.doc_part_gallery = Wordprocessingml::StructuredDocumentTag::DocPartGallery.new(
    value: "Bibliographies",
  )
  dpo.doc_part_unique = Wordprocessingml::StructuredDocumentTag::DocPartUnique.new
  sdt.properties.doc_part_obj = dpo
  sdt
end

.date(tag: nil, format: "M/d/yyyy", locale: "en-US", calendar: "gregorian") ⇒ SdtBuilder

Create a date picker content control

Parameters:

  • tag (String, nil) (defaults to: nil)

    Developer tag

  • format (String) (defaults to: "M/d/yyyy")

    Date format (default ‘M/d/yyyy’)

  • locale (String) (defaults to: "en-US")

    Locale (default ‘en-US’)

  • calendar (String) (defaults to: "gregorian")

    Calendar type (default ‘gregorian’)

Returns:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/uniword/builder/sdt_builder.rb', line 125

def self.date(tag: nil, format: "M/d/yyyy", locale: "en-US",
              calendar: "gregorian")
  sdt = new
  sdt.tag(tag) if tag

  date = Wordprocessingml::StructuredDocumentTag::Date.new
  date.date_format = Wordprocessingml::StructuredDocumentTag::DateFormat.new(
    value: format,
  )
  date.lid = Wordprocessingml::StructuredDocumentTag::Lid.new(value: locale)
  date.store_mapped_data_as = Wordprocessingml::StructuredDocumentTag::StoreMappedDataAs.new(
    value: "dateTime",
  )
  date.calendar = Wordprocessingml::StructuredDocumentTag::Calendar.new(
    value: calendar,
  )
  sdt.properties.date = date
  sdt
end

.default_model_classObject



22
23
24
# File 'lib/uniword/builder/sdt_builder.rb', line 22

def self.default_model_class
  Wordprocessingml::StructuredDocumentTag
end

.doc_part(gallery:, unique: true) ⇒ SdtBuilder

Create a document part content control (e.g., Table of Contents)

Parameters:

  • gallery (String)

    Gallery name (e.g., ‘Table of Contents’)

  • unique (Boolean) (defaults to: true)

    Mark as unique

Returns:



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/uniword/builder/sdt_builder.rb', line 167

def self.doc_part(gallery:, unique: true)
  sdt = new

  dpo = Wordprocessingml::StructuredDocumentTag::DocPartObj.new
  dpo.doc_part_gallery = Wordprocessingml::StructuredDocumentTag::DocPartGallery.new(
    value: gallery,
  )
  dpo.doc_part_unique = Wordprocessingml::StructuredDocumentTag::DocPartUnique.new if unique
  sdt.properties.doc_part_obj = dpo
  sdt
end

.text(tag: nil, alias_name: nil, placeholder_text: nil, lock: false) ⇒ SdtBuilder

Create a plain text content control

Parameters:

  • tag (String, nil) (defaults to: nil)

    Developer tag

  • alias_name (String, nil) (defaults to: nil)

    Display name

  • placeholder_text (String, nil) (defaults to: nil)

    Placeholder text

  • lock (Boolean) (defaults to: false)

    Lock content

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/uniword/builder/sdt_builder.rb', line 105

def self.text(tag: nil, alias_name: nil, placeholder_text: nil,
lock: false)
  sdt = new
  sdt.tag(tag) if tag
  sdt.alias(alias_name) if alias_name
  sdt.lock if lock

  sdt.properties.text = Wordprocessingml::StructuredDocumentTag::Text.new

  sdt.showing_placeholder if placeholder_text
  sdt
end

Instance Method Details

#alias(value) ⇒ self

Set the display alias

Parameters:

  • value (String)

    Alias name

Returns:

  • (self)


57
58
59
60
61
62
# File 'lib/uniword/builder/sdt_builder.rb', line 57

def alias(value)
  properties.alias_name = Wordprocessingml::StructuredDocumentTag::Alias.new(
    value: value,
  )
  self
end

#buildObject



82
83
84
85
86
87
88
89
# File 'lib/uniword/builder/sdt_builder.rb', line 82

def build
  unless properties.id
    properties.id = Wordprocessingml::StructuredDocumentTag::Id.new(
      value: next_id,
    )
  end
  @model
end

#id(value = nil) ⇒ self

Set the SDT identifier

Parameters:

  • value (Integer) (defaults to: nil)

    Unique ID (auto-generated if nil)

Returns:

  • (self)


35
36
37
38
39
40
# File 'lib/uniword/builder/sdt_builder.rb', line 35

def id(value = nil)
  properties.id = Wordprocessingml::StructuredDocumentTag::Id.new(
    value: value || next_id,
  )
  self
end

#lock(_value = true) ⇒ self

Set the lock / content cannot be edited

Parameters:

  • value (Boolean)

    Lock content (default true)

Returns:

  • (self)


68
69
70
71
# File 'lib/uniword/builder/sdt_builder.rb', line 68

def lock(_value = true)
  properties.temporary = Wordprocessingml::StructuredDocumentTag::Temporary.new
  self
end

#propertiesObject



91
92
93
94
# File 'lib/uniword/builder/sdt_builder.rb', line 91

def properties
  @model.properties ||= Wordprocessingml::StructuredDocumentTagProperties.new
  @model.properties
end

#showing_placeholder(_value = true) ⇒ self

Set placeholder text showing the placeholder header

Parameters:

  • value (Boolean)

    Show placeholder (default true)

Returns:

  • (self)


77
78
79
80
# File 'lib/uniword/builder/sdt_builder.rb', line 77

def showing_placeholder(_value = true)
  properties.showing_placeholder_header = Wordprocessingml::StructuredDocumentTag::ShowingPlaceholderHeader.new
  self
end

#tag(value) ⇒ self

Set the developer tag

Parameters:

  • value (String)

    Tag name

Returns:

  • (self)


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

def tag(value)
  properties.tag = Wordprocessingml::StructuredDocumentTag::Tag.new(
    value: value,
  )
  self
end