Class: Uniword::Transformation::ImageTransformationRule

Inherits:
TransformationRule show all
Defined in:
lib/uniword/transformation/image_transformation_rule.rb

Overview

Transformation rule for Image elements.

Responsibility: Transform Image objects between DOCX and MHTML formats. Single Responsibility - handles only Image transformations.

Transforms image properties including positioning, dimensions, and metadata.

Examples:

Transform DOCX image to MHTML

rule = ImageTransformationRule.new(source_format: :docx, target_format: :mhtml)
mhtml_image = rule.transform(docx_image)

Instance Attribute Summary

Attributes inherited from TransformationRule

#source_format, #target_format

Instance Method Summary collapse

Methods inherited from TransformationRule

#initialize

Constructor Details

This class inherits a constructor from Uniword::Transformation::TransformationRule

Instance Method Details

#matches?(element_type:, source_format:, target_format:) ⇒ Boolean

Check if this rule matches the transformation request

Parameters:

  • element_type (Class)

    The element class

  • source_format (Symbol)

    Source format

  • target_format (Symbol)

    Target format

Returns:

  • (Boolean)

    true if matches



22
23
24
25
26
# File 'lib/uniword/transformation/image_transformation_rule.rb', line 22

def matches?(element_type:, source_format:, target_format:)
  element_type == Image &&
    source_format == @source_format &&
    target_format == @target_format
end

#transform(source_image) ⇒ Image

Transform an image from source format to target format

Creates new Image with adapted properties.

Parameters:

  • source_image (Image)

    Source image

Returns:

  • (Image)

    Transformed image

Raises:

  • (ArgumentError)

    if source_image is not an Image



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/uniword/transformation/image_transformation_rule.rb', line 35

def transform(source_image)
  validate_element_type(source_image, Image)

  # Create new image with core properties
  target_image = Image.new(
    relationship_id: source_image.relationship_id,
    width: source_image.width,
    height: source_image.height,
    filename: source_image.filename,
    alt_text: source_image.alt_text,
    title: source_image.title,
  )

  # Transform positioning properties
  transform_positioning(source_image, target_image)

  target_image
end