Class: Uniword::Batch::ConvertFormatStage

Inherits:
ProcessingStage show all
Defined in:
lib/uniword/batch/stages/convert_format_stage.rb

Overview

Processing stage that converts document format.

Responsibility: Convert documents between DOCX and MHTML formats. Single Responsibility - only handles format conversion.

Examples:

Use in pipeline

stage = ConvertFormatStage.new(
  target_format: :docx,
  preserve_original: true
)
document = stage.process(document, context)

Instance Attribute Summary

Attributes inherited from ProcessingStage

#enabled, #options

Instance Method Summary collapse

Methods inherited from ProcessingStage

#enabled?, #name

Constructor Details

#initialize(options = {}) ⇒ ConvertFormatStage

Initialize convert format stage

Parameters:

  • options (Hash) (defaults to: {})

    Stage options

Options Hash (options):

  • :target_format (Symbol)

    Target format (:docx or :mhtml)

  • :preserve_original (Boolean)

    Keep original format file



22
23
24
25
26
27
28
# File 'lib/uniword/batch/stages/convert_format_stage.rb', line 22

def initialize(options = {})
  super
  @target_format = options.fetch(:target_format, :docx)
  @preserve_original = options.fetch(:preserve_original, true)

  validate_target_format!
end

Instance Method Details

#descriptionString

Get stage description

Returns:

  • (String)

    Description



62
63
64
# File 'lib/uniword/batch/stages/convert_format_stage.rb', line 62

def description
  "Convert document format to #{@target_format}"
end

#process(document, context = {}) ⇒ Document

Process document to convert format

Parameters:

  • document (Document)

    Document to process

  • context (Hash) (defaults to: {})

    Processing context

Returns:

  • (Document)

    Processed document



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/uniword/batch/stages/convert_format_stage.rb', line 35

def process(document, context = {})
  log "Converting to #{@target_format} format: #{context[:filename]}"

  # Detect current format from file extension
  current_format = detect_format(context[:input_path])

  # Skip if already in target format
  if current_format == @target_format
    log "Document already in #{@target_format} format, skipping conversion"
    return document
  end

  # Update output path extension if needed
  if context[:output_path]
    context[:output_path] = update_output_extension(
      context[:output_path],
      @target_format,
    )
  end

  log "Format conversion complete"
  document
end