Class: Uniword::Ooxml::PartDefinition
- Inherits:
-
Object
- Object
- Uniword::Ooxml::PartDefinition
- Defined in:
- lib/uniword/ooxml/part_definition.rb
Overview
Declarative description of one OOXML package part kind.
A PartDefinition is the single source of truth for a part's package path, content type, relationship type, the form its [Content_Types].xml entry takes (Default by extension or Override by part name), how it is loaded from extracted ZIP content, and where it lives on both Wordprocessingml::DocumentRoot and Docx::Package. Instances are immutable value objects registered in PartRegistry; consumers derive every literal from them.
Parts with instance-numbered paths (headers, footers, charts,
customXml items) carry a +path_pattern+/+target_pattern+ with
+format+-style placeholders (e.g. "word/header%
Load behavior beyond this data (parse rules for special part
kinds) lives in named Docx::PartLoader strategy classes selected
by loader; the definition itself stays a value object.
Instance Attribute Summary collapse
-
#content_type ⇒ String?
readonly
MIME content type (nil when dynamic, e.g. images whose type depends on the file extension).
-
#copy_to_document ⇒ Boolean
readonly
Whether the package→document copy includes this part (false when the loader already places the part on the document, or the part is never read from ZIP content).
-
#document_attribute ⇒ Symbol?
readonly
Attribute (reader/writer) holding this part on Wordprocessingml::DocumentRoot.
-
#extension ⇒ String?
readonly
File extension for Default entries.
-
#key ⇒ Symbol
readonly
Unique registry key (e.g. :styles).
-
#kind ⇒ Symbol
readonly
:override, :default, or :none (no [Content_Types].xml entry of its own).
-
#load_priority ⇒ Integer?
readonly
Load ordering weight — lower loads first; entries sharing a priority load in registration order.
-
#loader ⇒ Symbol?
readonly
Docx::PartLoader strategy key (:xml_model, :custom_xml, :header_footer, :chart, :image, :embedding, :theme_media); nil when the part is not loaded from ZIP content.
-
#loader_model ⇒ Class?
readonly
Model class parsing the part's XML (responds to
.from_xml); used by loaders that parse. -
#package_attribute ⇒ Symbol?
readonly
Attribute (reader/writer) holding this part on Docx::Package.
-
#path ⇒ String?
readonly
Package-relative path ("word/styles.xml").
-
#path_pattern ⇒ String?
readonly
Path template for instance-numbered parts.
-
#path_resolution ⇒ Symbol?
readonly
Dynamic path resolution rule: :office_document (resolve the main document path from the package-level officeDocument relationship) or :office_document_rels (sidecar .rels of the resolved main document); nil for fixed paths.
-
#rel_type ⇒ String?
readonly
Relationship type URI (nil when the part is not referenced by a relationship).
-
#rels_scope ⇒ Symbol?
readonly
:package or :document — which .rels part the relationship lives in (nil when
rel_typeis nil). -
#required ⇒ Boolean
readonly
Whether a minimal valid package carries it.
-
#standard ⇒ Boolean
readonly
Whether ContentTypes.generate includes it in the comprehensive [Content_Types].xml for new DOCX packages.
-
#target ⇒ String?
readonly
Relationship Target as written in .rels (relative to the owning part; defaults to
path). -
#target_pattern ⇒ String?
readonly
Target template for numbered parts.
-
#to_package_guard ⇒ Symbol?
readonly
Predicate on the document guarding the document→package copy (e.g. :numbering_configuration_loaded? for the lazily-initialized numbering configuration).
-
#to_package_type ⇒ Class?
readonly
Class the value must be an instance of for the document→package copy (e.g. CommentsPart, since DocumentRoot#comments can hold non-part values).
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two definitions are equal when every field matches.
-
#copy_to_document? ⇒ Boolean
True when the package→document copy mirrors this part onto Wordprocessingml::DocumentRoot.
-
#default? ⇒ Boolean
True for [Content_Types].xml Default entries.
-
#emitted_paths(package) ⇒ Array<String>
Package paths this part kind emits for the given package.
-
#initialize(key:, kind:, path: nil, path_pattern: nil, target: nil, target_pattern: nil, content_type: nil, rel_type: nil, extension: nil, required: false, rels_scope: nil, standard: false, loader: nil, loader_model: nil, path_resolution: nil, load_priority: nil, document_attribute: nil, package_attribute: nil, copy_to_document: true, to_package_guard: nil, to_package_type: nil) ⇒ PartDefinition
constructor
rubocop:disable Metrics/ParameterLists, Metrics/MethodLength rubocop:disable Metrics/AbcSize.
-
#loadable? ⇒ Boolean
True when the part loads from ZIP content.
-
#match_path?(candidate) ⇒ Boolean
Whether a package path matches this definition's fixed path or path pattern.
-
#override? ⇒ Boolean
True for [Content_Types].xml Override entries.
-
#package_rel? ⇒ Boolean
True when the relationship lives in _rels/.rels.
-
#part_name ⇒ String?
Fixed-path Override part name (leading slash).
-
#part_name_for(**vars) ⇒ String?
Resolve the content-type Override part name (leading slash).
-
#path_for(**vars) ⇒ String?
Resolve the package-relative path, interpolating pattern placeholders for instance-numbered parts.
-
#pattern_prefix ⇒ String?
Static prefix of the path pattern up to the first placeholder (e.g. "word/theme/media/" for "word/theme/media/%
s"). -
#required? ⇒ Boolean
True when a minimal valid package carries it.
-
#standard? ⇒ Boolean
True when ContentTypes.generate includes it.
-
#target_for(**vars) ⇒ String?
Resolve the relationship Target attribute.
Constructor Details
#initialize(key:, kind:, path: nil, path_pattern: nil, target: nil, target_pattern: nil, content_type: nil, rel_type: nil, extension: nil, required: false, rels_scope: nil, standard: false, loader: nil, loader_model: nil, path_resolution: nil, load_priority: nil, document_attribute: nil, package_attribute: nil, copy_to_document: true, to_package_guard: nil, to_package_type: nil) ⇒ PartDefinition
rubocop:disable Metrics/ParameterLists, Metrics/MethodLength rubocop:disable Metrics/AbcSize
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/uniword/ooxml/part_definition.rb', line 125 def initialize(key:, kind:, path: nil, path_pattern: nil, target: nil, target_pattern: nil, content_type: nil, rel_type: nil, extension: nil, required: false, rels_scope: nil, standard: false, loader: nil, loader_model: nil, path_resolution: nil, load_priority: nil, document_attribute: nil, package_attribute: nil, copy_to_document: true, to_package_guard: nil, to_package_type: nil) @key = key.to_sym @kind = kind @path = path @path_pattern = path_pattern @target = target || path @target_pattern = target_pattern @content_type = content_type @rel_type = rel_type @extension = extension @required = required @rels_scope = rels_scope @standard = standard @loader = loader @loader_model = loader_model @path_resolution = path_resolution @load_priority = load_priority @document_attribute = document_attribute @package_attribute = package_attribute @copy_to_document = copy_to_document @to_package_guard = to_package_guard @to_package_type = to_package_type end |
Instance Attribute Details
#content_type ⇒ String? (readonly)
Returns MIME content type (nil when dynamic, e.g. images whose type depends on the file extension).
55 56 57 |
# File 'lib/uniword/ooxml/part_definition.rb', line 55 def content_type @content_type end |
#copy_to_document ⇒ Boolean (readonly)
Returns whether the package→document copy includes this part (false when the loader already places the part on the document, or the part is never read from ZIP content).
111 112 113 |
# File 'lib/uniword/ooxml/part_definition.rb', line 111 def copy_to_document @copy_to_document end |
#document_attribute ⇒ Symbol? (readonly)
Returns attribute (reader/writer) holding this part on Wordprocessingml::DocumentRoot.
102 103 104 |
# File 'lib/uniword/ooxml/part_definition.rb', line 102 def document_attribute @document_attribute end |
#extension ⇒ String? (readonly)
Returns file extension for Default entries.
62 63 64 |
# File 'lib/uniword/ooxml/part_definition.rb', line 62 def extension @extension end |
#key ⇒ Symbol (readonly)
Returns unique registry key (e.g. :styles).
38 39 40 |
# File 'lib/uniword/ooxml/part_definition.rb', line 38 def key @key end |
#kind ⇒ Symbol (readonly)
Returns :override, :default, or :none (no [Content_Types].xml entry of its own).
69 70 71 |
# File 'lib/uniword/ooxml/part_definition.rb', line 69 def kind @kind end |
#load_priority ⇒ Integer? (readonly)
Returns load ordering weight — lower loads first; entries sharing a priority load in registration order.
98 99 100 |
# File 'lib/uniword/ooxml/part_definition.rb', line 98 def load_priority @load_priority end |
#loader ⇒ Symbol? (readonly)
Returns Docx::PartLoader strategy key (:xml_model, :custom_xml, :header_footer, :chart, :image, :embedding, :theme_media); nil when the part is not loaded from ZIP content.
83 84 85 |
# File 'lib/uniword/ooxml/part_definition.rb', line 83 def loader @loader end |
#loader_model ⇒ Class? (readonly)
Returns model class parsing the part's XML
(responds to .from_xml); used by loaders that parse.
87 88 89 |
# File 'lib/uniword/ooxml/part_definition.rb', line 87 def loader_model @loader_model end |
#package_attribute ⇒ Symbol? (readonly)
Returns attribute (reader/writer) holding this part on Docx::Package.
106 107 108 |
# File 'lib/uniword/ooxml/part_definition.rb', line 106 def package_attribute @package_attribute end |
#path ⇒ String? (readonly)
Returns package-relative path ("word/styles.xml").
41 42 43 |
# File 'lib/uniword/ooxml/part_definition.rb', line 41 def path @path end |
#path_pattern ⇒ String? (readonly)
Returns path template for instance-numbered parts.
44 45 46 |
# File 'lib/uniword/ooxml/part_definition.rb', line 44 def path_pattern @path_pattern end |
#path_resolution ⇒ Symbol? (readonly)
Returns dynamic path resolution rule: :office_document (resolve the main document path from the package-level officeDocument relationship) or :office_document_rels (sidecar .rels of the resolved main document); nil for fixed paths.
94 95 96 |
# File 'lib/uniword/ooxml/part_definition.rb', line 94 def path_resolution @path_resolution end |
#rel_type ⇒ String? (readonly)
Returns relationship type URI (nil when the part is not referenced by a relationship).
59 60 61 |
# File 'lib/uniword/ooxml/part_definition.rb', line 59 def rel_type @rel_type end |
#rels_scope ⇒ Symbol? (readonly)
Returns :package or :document — which .rels part
the relationship lives in (nil when rel_type is nil).
73 74 75 |
# File 'lib/uniword/ooxml/part_definition.rb', line 73 def rels_scope @rels_scope end |
#required ⇒ Boolean (readonly)
Returns whether a minimal valid package carries it.
65 66 67 |
# File 'lib/uniword/ooxml/part_definition.rb', line 65 def required @required end |
#standard ⇒ Boolean (readonly)
Returns whether ContentTypes.generate includes it in the comprehensive [Content_Types].xml for new DOCX packages.
77 78 79 |
# File 'lib/uniword/ooxml/part_definition.rb', line 77 def standard @standard end |
#target ⇒ String? (readonly)
Returns relationship Target as written in .rels
(relative to the owning part; defaults to path).
48 49 50 |
# File 'lib/uniword/ooxml/part_definition.rb', line 48 def target @target end |
#target_pattern ⇒ String? (readonly)
Returns Target template for numbered parts.
51 52 53 |
# File 'lib/uniword/ooxml/part_definition.rb', line 51 def target_pattern @target_pattern end |
#to_package_guard ⇒ Symbol? (readonly)
Returns predicate on the document guarding the document→package copy (e.g. :numbering_configuration_loaded? for the lazily-initialized numbering configuration).
116 117 118 |
# File 'lib/uniword/ooxml/part_definition.rb', line 116 def to_package_guard @to_package_guard end |
#to_package_type ⇒ Class? (readonly)
Returns class the value must be an instance of for the document→package copy (e.g. CommentsPart, since DocumentRoot#comments can hold non-part values).
121 122 123 |
# File 'lib/uniword/ooxml/part_definition.rb', line 121 def to_package_type @to_package_type end |
Instance Method Details
#==(other) ⇒ Object
Two definitions are equal when every field matches.
266 267 268 |
# File 'lib/uniword/ooxml/part_definition.rb', line 266 def ==(other) other.is_a?(PartDefinition) && fields == other.fields end |
#copy_to_document? ⇒ Boolean
Returns true when the package→document copy mirrors this part onto Wordprocessingml::DocumentRoot.
190 191 192 |
# File 'lib/uniword/ooxml/part_definition.rb', line 190 def copy_to_document? !!copy_to_document end |
#default? ⇒ Boolean
Returns true for [Content_Types].xml Default entries.
164 165 166 |
# File 'lib/uniword/ooxml/part_definition.rb', line 164 def default? kind == :default end |
#emitted_paths(package) ⇒ Array<String>
Package paths this part kind emits for the given package.
Single-instance kinds emit their fixed path when the backing
model is present; collection families emit each stored part's
own paths (each part object answers package_paths).
258 259 260 261 262 263 |
# File 'lib/uniword/ooxml/part_definition.rb', line 258 def emitted_paths(package) source = attribute_source(package) return [] if source.nil? collection? ? collection_paths(source) : [path] end |
#loadable? ⇒ Boolean
Returns true when the part loads from ZIP content.
184 185 186 |
# File 'lib/uniword/ooxml/part_definition.rb', line 184 def loadable? !loader.nil? end |
#match_path?(candidate) ⇒ Boolean
Whether a package path matches this definition's fixed path or path pattern. Accepts both "word/styles.xml" and "/word/styles.xml" forms, and matches numbered paths against the pattern ("word/header2.xml" → :header).
201 202 203 204 205 206 207 |
# File 'lib/uniword/ooxml/part_definition.rb', line 201 def match_path?(candidate) normalized = candidate.to_s.delete_prefix("/") return true if path == normalized return false unless path_pattern normalized.match?(template_regex(path_pattern)) end |
#override? ⇒ Boolean
Returns true for [Content_Types].xml Override entries.
159 160 161 |
# File 'lib/uniword/ooxml/part_definition.rb', line 159 def override? kind == :override end |
#package_rel? ⇒ Boolean
Returns true when the relationship lives in _rels/.rels.
179 180 181 |
# File 'lib/uniword/ooxml/part_definition.rb', line 179 def package_rel? rels_scope == :package end |
#part_name ⇒ String?
Fixed-path Override part name (leading slash).
246 247 248 |
# File 'lib/uniword/ooxml/part_definition.rb', line 246 def part_name path && "/#{path}" end |
#part_name_for(**vars) ⇒ String?
Resolve the content-type Override part name (leading slash).
230 231 232 233 |
# File 'lib/uniword/ooxml/part_definition.rb', line 230 def part_name_for(**vars) resolved = path_for(**vars) resolved && "/#{resolved}" end |
#path_for(**vars) ⇒ String?
Resolve the package-relative path, interpolating pattern placeholders for instance-numbered parts.
222 223 224 |
# File 'lib/uniword/ooxml/part_definition.rb', line 222 def path_for(**vars) (path_pattern || path, vars) end |
#pattern_prefix ⇒ String?
Static prefix of the path pattern up to the first placeholder
(e.g. "word/theme/media/" for "word/theme/media/%
213 214 215 |
# File 'lib/uniword/ooxml/part_definition.rb', line 213 def pattern_prefix path_pattern&.split(/%<\w+>[ds]/)&.first end |
#required? ⇒ Boolean
Returns true when a minimal valid package carries it.
169 170 171 |
# File 'lib/uniword/ooxml/part_definition.rb', line 169 def required? !!required end |
#standard? ⇒ Boolean
Returns true when ContentTypes.generate includes it.
174 175 176 |
# File 'lib/uniword/ooxml/part_definition.rb', line 174 def standard? !!standard end |
#target_for(**vars) ⇒ String?
Resolve the relationship Target attribute.
239 240 241 |
# File 'lib/uniword/ooxml/part_definition.rb', line 239 def target_for(**vars) (target_pattern || target, vars) end |