Class: Uniword::Docx::Part
- Inherits:
-
Object
- Object
- Uniword::Docx::Part
- Defined in:
- lib/uniword/docx/part.rb
Overview
Value object for one package-held part (chart, embedding, header, footer, ...).
A Part couples the part's content (a lutaml-model object or a raw String) with the packaging metadata the OPC layer needs to emit it: relationship target, relationship id, content type and relationship type. Defaults for the metadata come from the part's Ooxml::PartDefinition (the single source of truth); loaded documents may carry verbatim overrides (e.g. strict-OOXML relationship URIs).
Replaces the raw Hash entries previously stored in
chart_parts, embeddings and header_footer_parts. Hash-style
read access (+part+) is kept for backward compatibility.
Direct Known Subclasses
Constant Summary collapse
- HASH_LOOKUP =
Key → reader lambdas backing hash-style access (kept as data so the dispatch stays declarative).
{ r_id: lambda(&:r_id), target: lambda(&:target), rel_type: lambda(&:rel_type), content_type: lambda(&:content_type), content: lambda(&:content), path: lambda(&:path), }.freeze
Instance Attribute Summary collapse
-
#content ⇒ Object
Part content (model object or raw String).
-
#content_type ⇒ String?
MIME content type.
-
#definition ⇒ Ooxml::PartDefinition?
readonly
Registry definition for this part kind.
-
#r_id ⇒ String?
Relationship id (nil until wired).
-
#rel_type ⇒ String?
Relationship type URI.
-
#target ⇒ String?
Relationship target relative to word/.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Part
Wrap a legacy raw-hash entry ({ content:/xml:, target: }) into a Part.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Hash-style read compatibility for former hash entries.
-
#initialize(definition: nil, r_id: nil, target: nil, content: nil, rel_type: nil, content_type: nil) ⇒ Part
constructor
rubocop:disable Metrics/ParameterLists.
-
#package_paths ⇒ Array<String>
Package paths this part emits (one for plain parts; subclasses with sidecar parts override).
-
#path ⇒ String?
Package-relative path of the emitted part.
Constructor Details
#initialize(definition: nil, r_id: nil, target: nil, content: nil, rel_type: nil, content_type: nil) ⇒ Part
rubocop:disable Metrics/ParameterLists
52 53 54 55 56 57 58 59 60 |
# File 'lib/uniword/docx/part.rb', line 52 def initialize(definition: nil, r_id: nil, target: nil, content: nil, rel_type: nil, content_type: nil) @definition = definition @r_id = r_id @target = target @content = content @rel_type = rel_type @content_type = content_type end |
Instance Attribute Details
#content ⇒ Object
Returns part content (model object or raw String).
40 41 42 |
# File 'lib/uniword/docx/part.rb', line 40 def content @content end |
#content_type ⇒ String?
Returns MIME content type.
80 81 82 |
# File 'lib/uniword/docx/part.rb', line 80 def content_type @content_type || definition&.content_type end |
#definition ⇒ Ooxml::PartDefinition? (readonly)
Returns registry definition for this part kind.
31 32 33 |
# File 'lib/uniword/docx/part.rb', line 31 def definition @definition end |
#r_id ⇒ String?
Returns relationship id (nil until wired).
34 35 36 |
# File 'lib/uniword/docx/part.rb', line 34 def r_id @r_id end |
#rel_type ⇒ String?
Returns relationship type URI.
75 76 77 |
# File 'lib/uniword/docx/part.rb', line 75 def rel_type @rel_type || definition&.rel_type end |
#target ⇒ String?
Returns relationship target relative to word/.
37 38 39 |
# File 'lib/uniword/docx/part.rb', line 37 def target @target end |
Class Method Details
.from_hash(hash) ⇒ Part
Wrap a legacy raw-hash entry ({ content:/xml:, target: }) into a Part. Subclasses override for family-specific keys (e.g. ImagePart's { data:, content_type:, path: }). Used by PartCollection to normalize hash assignments.
70 71 72 |
# File 'lib/uniword/docx/part.rb', line 70 def self.from_hash(hash) new(target: hash[:target], content: hash[:content] || hash[:xml]) end |
Instance Method Details
#[](key) ⇒ Object?
Hash-style read compatibility for former hash entries.
120 121 122 |
# File 'lib/uniword/docx/part.rb', line 120 def [](key) HASH_LOOKUP[key.to_sym]&.call(self) end |
#package_paths ⇒ Array<String>
Package paths this part emits (one for plain parts; subclasses with sidecar parts override).
97 98 99 |
# File 'lib/uniword/docx/part.rb', line 97 def package_paths [path].compact end |
#path ⇒ String?
Package-relative path of the emitted part. Document-scoped parts live under word/; their target is already relative to word/.
89 90 91 |
# File 'lib/uniword/docx/part.rb', line 89 def path target && "word/#{target}" end |