Class: Uniword::Docx::ImagePart

Inherits:
Part
  • Object
show all
Defined in:
lib/uniword/docx/image_part.rb

Overview

An image part (word/media/*) held by the package: binary image data plus the packaging metadata needed to emit it (relationship target, relationship id, content type).

The image content type is per-file (resolved from the file extension), so it is always carried explicitly — the registry's :image definition contributes only the relationship type.

Replaces the raw { data:, target:, content_type:, path: } hash entries formerly stored in DocumentRoot#image_parts. Hash-style read access (+part+, part[:target], part[:content_type], part[:path]) is kept for backward compatibility.

Examples:

part = ImagePart.new(
  r_id: "rId5", target: "media/image1.png",
  data: binary_data, content_type: "image/png",
)
part.data         # => binary image data
part.content_type # => "image/png"

Constant Summary

Constants inherited from Part

Part::HASH_LOOKUP

Instance Attribute Summary collapse

Attributes inherited from Part

#content, #content_type, #definition, #r_id, #rel_type, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Part

#package_paths, #path

Constructor Details

#initialize(r_id: nil, target: nil, data: nil, content_type: nil, source_path: nil, **rest) ⇒ ImagePart

rubocop:disable Metrics/ParameterLists

Parameters:

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

    relationship id

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

    e.g. "media/image1.png"

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

    binary image data

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

    e.g. "image/png"

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

    on-disk source path



36
37
38
39
40
41
42
43
44
# File 'lib/uniword/docx/image_part.rb', line 36

def initialize(r_id: nil, target: nil, data: nil, content_type: nil,
               source_path: nil, **rest)
  super(
    definition: Ooxml::PartRegistry.find_by_key(:image),
    r_id: r_id, target: target, content: data,
    content_type: content_type, **rest
  )
  @source_path = source_path
end

Instance Attribute Details

#source_pathString?

Returns path of the source file the image was read from (builder-added images only; nil for loaded parts).

Returns:

  • (String, nil)

    path of the source file the image was read from (builder-added images only; nil for loaded parts)



28
29
30
# File 'lib/uniword/docx/image_part.rb', line 28

def source_path
  @source_path
end

Class Method Details

.from_hash(hash) ⇒ ImagePart

Wrap a legacy raw-hash entry ({ data:, target:, content_type:, path: }) into an ImagePart.

Parameters:

  • hash (Hash)

    legacy hash entry

Returns:



52
53
54
55
# File 'lib/uniword/docx/image_part.rb', line 52

def self.from_hash(hash)
  new(target: hash[:target], data: hash[:data],
      content_type: hash[:content_type], source_path: hash[:path])
end

Instance Method Details

#[](key) ⇒ Object

Hash-style read compatibility (+:data+ and the legacy :path source path in addition to the Part keys).



74
75
76
77
78
79
80
# File 'lib/uniword/docx/image_part.rb', line 74

def [](key)
  case key.to_sym
  when :data then content
  when :path then source_path
  else super
  end
end

#dataString?

Binary image data (alias for content).

Returns:

  • (String, nil)


60
61
62
# File 'lib/uniword/docx/image_part.rb', line 60

def data
  content
end

#data=(value) ⇒ void

This method returns an undefined value.

Set the binary image data (alias for content=).

Parameters:

  • value (String, nil)

    binary image data



68
69
70
# File 'lib/uniword/docx/image_part.rb', line 68

def data=(value)
  self.content = value
end