Class: Uniword::Transformation::RunTransformationRule

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

Overview

Transformation rule for Run elements.

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

Transformations preserve text content and formatting properties. Format-specific properties are adapted as needed.

Examples:

Transform DOCX run to MHTML

rule = RunTransformationRule.new(source_format: :docx, target_format: :mhtml)
mhtml_run = rule.transform(docx_run)

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



23
24
25
26
27
# File 'lib/uniword/transformation/run_transformation_rule.rb', line 23

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

#transform(source_run) ⇒ Run

Transform a run from source format to target format

Creates new Run with same text and adapted properties.

Parameters:

  • source_run (Run)

    Source run

Returns:

  • (Run)

    Transformed run

Raises:

  • (ArgumentError)

    if source_run is not a Run



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/uniword/transformation/run_transformation_rule.rb', line 36

def transform(source_run)
  validate_element_type(source_run, Run)

  # Create new run with same text
  target_run = Run.new(text: source_run.text)

  # Transform properties if they exist
  transform_properties(source_run, target_run) if source_run.properties

  target_run
end