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

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

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



69
70
71
# File 'lib/uniword/docx/part.rb', line 69

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



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

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

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)


101
102
103
# File 'lib/uniword/docx/part.rb', line 101

def [](key)
  HASH_LOOKUP[key.to_sym]&.call(self)
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"



78
79
80
# File 'lib/uniword/docx/part.rb', line 78

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