Class: Uniword::Docx::Part

Inherits:
Object
  • Object
show all
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.

Examples:

part = Part.new(
  definition: Ooxml::PartRegistry.find_by_key(:ole_object),
  target: "embeddings/file.xlsx",
  content: binary_data,
)
part.path         # => "word/embeddings/file.xlsx"
part.content_type # => definition's content type

Direct Known Subclasses

ChartPart, HeaderFooterPart, ImagePart, RawPart

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition: nil, r_id: nil, target: nil, content: nil, rel_type: nil, content_type: nil) ⇒ Part

rubocop:disable Metrics/ParameterLists

Parameters:

  • definition (Ooxml::PartDefinition, nil) (defaults to: nil)

    registry entry

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

    relationship id

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

    relationship target (relative to the owning part, e.g. "charts/chart1.xml")

  • content (Object, nil) (defaults to: nil)

    part content

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

    explicit relationship type override (verbatim value from a loaded package)

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

    explicit content type override (verbatim value from a loaded package)



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

#contentObject

Returns part content (model object or raw String).

Returns:

  • (Object)

    part content (model object or raw String)



40
41
42
# File 'lib/uniword/docx/part.rb', line 40

def content
  @content
end

#content_typeString?

Returns MIME content type.

Returns:

  • (String, nil)

    MIME content type



80
81
82
# File 'lib/uniword/docx/part.rb', line 80

def content_type
  @content_type || definition&.content_type
end

#definitionOoxml::PartDefinition? (readonly)

Returns registry definition for this part kind.

Returns:



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

def definition
  @definition
end

#r_idString?

Returns relationship id (nil until wired).

Returns:

  • (String, nil)

    relationship id (nil until wired)



34
35
36
# File 'lib/uniword/docx/part.rb', line 34

def r_id
  @r_id
end

#rel_typeString?

Returns relationship type URI.

Returns:

  • (String, nil)

    relationship type URI



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

def rel_type
  @rel_type || definition&.rel_type
end

#targetString?

Returns relationship target relative to word/.

Returns:

  • (String, nil)

    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.

Parameters:

  • hash (Hash)

    legacy hash entry

Returns:



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.

Parameters:

  • key (Symbol, String)

    one of :r_id, :target, :rel_type, :content_type, :content, :path

Returns:

  • (Object, nil)


120
121
122
# File 'lib/uniword/docx/part.rb', line 120

def [](key)
  HASH_LOOKUP[key.to_sym]&.call(self)
end

#package_pathsArray<String>

Package paths this part emits (one for plain parts; subclasses with sidecar parts override).

Returns:

  • (Array<String>)

    emitted package paths



97
98
99
# File 'lib/uniword/docx/part.rb', line 97

def package_paths
  [path].compact
end

#pathString?

Package-relative path of the emitted part. Document-scoped parts live under word/; their target is already relative to word/.

Returns:

  • (String, nil)

    e.g. "word/charts/chart1.xml"



89
90
91
# File 'lib/uniword/docx/part.rb', line 89

def path
  target && "word/#{target}"
end