Class: Uniword::Ooxml::PartDefinition

Inherits:
Object
  • Object
show all
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, and the form its [Content_Types].xml entry takes (Default by extension or Override by part name). 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%d.xml") instead of a fixed path.

Examples:

Fixed part

PartDefinition.new(
  key: :styles, path: "word/styles.xml", target: "styles.xml",
  content_type: "...styles+xml",
  rel_type: ".../relationships/styles",
  required: true, kind: :override, rels_scope: :document,
)

Instance Attribute Summary collapse

Instance Method Summary collapse

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) ⇒ PartDefinition

rubocop:disable Metrics/ParameterLists



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/uniword/ooxml/part_definition.rb', line 69

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)
  @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
end

Instance Attribute Details

#content_typeString? (readonly)

Returns MIME content type (nil when dynamic, e.g. images whose type depends on the file extension).

Returns:

  • (String, nil)

    MIME content type (nil when dynamic, e.g. images whose type depends on the file extension)



44
45
46
# File 'lib/uniword/ooxml/part_definition.rb', line 44

def content_type
  @content_type
end

#extensionString? (readonly)

Returns file extension for Default entries.

Returns:

  • (String, nil)

    file extension for Default entries



51
52
53
# File 'lib/uniword/ooxml/part_definition.rb', line 51

def extension
  @extension
end

#keySymbol (readonly)

Returns unique registry key (e.g. :styles).

Returns:

  • (Symbol)

    unique registry key (e.g. :styles)



27
28
29
# File 'lib/uniword/ooxml/part_definition.rb', line 27

def key
  @key
end

#kindSymbol (readonly)

Returns :override, :default, or :none (no [Content_Types].xml entry of its own).

Returns:

  • (Symbol)

    :override, :default, or :none (no [Content_Types].xml entry of its own)



58
59
60
# File 'lib/uniword/ooxml/part_definition.rb', line 58

def kind
  @kind
end

#pathString? (readonly)

Returns package-relative path ("word/styles.xml").

Returns:

  • (String, nil)

    package-relative path ("word/styles.xml")



30
31
32
# File 'lib/uniword/ooxml/part_definition.rb', line 30

def path
  @path
end

#path_patternString? (readonly)

Returns path template for instance-numbered parts.

Returns:

  • (String, nil)

    path template for instance-numbered parts



33
34
35
# File 'lib/uniword/ooxml/part_definition.rb', line 33

def path_pattern
  @path_pattern
end

#rel_typeString? (readonly)

Returns relationship type URI (nil when the part is not referenced by a relationship).

Returns:

  • (String, nil)

    relationship type URI (nil when the part is not referenced by a relationship)



48
49
50
# File 'lib/uniword/ooxml/part_definition.rb', line 48

def rel_type
  @rel_type
end

#rels_scopeSymbol? (readonly)

Returns :package or :document — which .rels part the relationship lives in (nil when rel_type is nil).

Returns:

  • (Symbol, nil)

    :package or :document — which .rels part the relationship lives in (nil when rel_type is nil)



62
63
64
# File 'lib/uniword/ooxml/part_definition.rb', line 62

def rels_scope
  @rels_scope
end

#requiredBoolean (readonly)

Returns whether a minimal valid package carries it.

Returns:

  • (Boolean)

    whether a minimal valid package carries it



54
55
56
# File 'lib/uniword/ooxml/part_definition.rb', line 54

def required
  @required
end

#standardBoolean (readonly)

Returns whether ContentTypes.generate includes it in the comprehensive [Content_Types].xml for new DOCX packages.

Returns:

  • (Boolean)

    whether ContentTypes.generate includes it in the comprehensive [Content_Types].xml for new DOCX packages



66
67
68
# File 'lib/uniword/ooxml/part_definition.rb', line 66

def standard
  @standard
end

#targetString? (readonly)

Returns relationship Target as written in .rels (relative to the owning part; defaults to path).

Returns:

  • (String, nil)

    relationship Target as written in .rels (relative to the owning part; defaults to path)



37
38
39
# File 'lib/uniword/ooxml/part_definition.rb', line 37

def target
  @target
end

#target_patternString? (readonly)

Returns Target template for numbered parts.

Returns:

  • (String, nil)

    Target template for numbered parts



40
41
42
# File 'lib/uniword/ooxml/part_definition.rb', line 40

def target_pattern
  @target_pattern
end

Instance Method Details

#==(other) ⇒ Object

Two definitions are equal when every field matches.



147
148
149
# File 'lib/uniword/ooxml/part_definition.rb', line 147

def ==(other)
  other.is_a?(PartDefinition) && fields == other.fields
end

#default?Boolean

Returns true for [Content_Types].xml Default entries.

Returns:

  • (Boolean)

    true for [Content_Types].xml Default entries



94
95
96
# File 'lib/uniword/ooxml/part_definition.rb', line 94

def default?
  kind == :default
end

#override?Boolean

Returns true for [Content_Types].xml Override entries.

Returns:

  • (Boolean)

    true for [Content_Types].xml Override entries



89
90
91
# File 'lib/uniword/ooxml/part_definition.rb', line 89

def override?
  kind == :override
end

#package_rel?Boolean

Returns true when the relationship lives in _rels/.rels.

Returns:

  • (Boolean)

    true when the relationship lives in _rels/.rels



109
110
111
# File 'lib/uniword/ooxml/part_definition.rb', line 109

def package_rel?
  rels_scope == :package
end

#part_nameString?

Fixed-path Override part name (leading slash).

Returns:

  • (String, nil)

    e.g. "/word/styles.xml"



142
143
144
# File 'lib/uniword/ooxml/part_definition.rb', line 142

def part_name
  path && "/#{path}"
end

#part_name_for(**vars) ⇒ String?

Resolve the content-type Override part name (leading slash).

Parameters:

  • vars (Hash)

    placeholder values for numbered parts

Returns:

  • (String, nil)

    e.g. "/word/header1.xml"



126
127
128
129
# File 'lib/uniword/ooxml/part_definition.rb', line 126

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.

Parameters:

  • vars (Hash)

    placeholder values (e.g. counter: 1)

Returns:

  • (String, nil)

    e.g. "word/header1.xml"



118
119
120
# File 'lib/uniword/ooxml/part_definition.rb', line 118

def path_for(**vars)
  expand(path_pattern || path, vars)
end

#required?Boolean

Returns true when a minimal valid package carries it.

Returns:

  • (Boolean)

    true when a minimal valid package carries it



99
100
101
# File 'lib/uniword/ooxml/part_definition.rb', line 99

def required?
  !!required
end

#standard?Boolean

Returns true when ContentTypes.generate includes it.

Returns:

  • (Boolean)

    true when ContentTypes.generate includes it



104
105
106
# File 'lib/uniword/ooxml/part_definition.rb', line 104

def standard?
  !!standard
end

#target_for(**vars) ⇒ String?

Resolve the relationship Target attribute.

Parameters:

  • vars (Hash)

    placeholder values for numbered parts

Returns:

  • (String, nil)

    e.g. "header1.xml"



135
136
137
# File 'lib/uniword/ooxml/part_definition.rb', line 135

def target_for(**vars)
  expand(target_pattern || target, vars)
end