Class: Uniword::FormatConverter
- Inherits:
-
Object
- Object
- Uniword::FormatConverter
- Defined in:
- lib/uniword/format_converter.rb
Overview
Explicit format converter with declarative API.
Responsibility: Coordinate format conversion using model transformation. Single Responsibility - orchestrates conversion, delegates implementation.
Architecture follows separation of concerns:
-
Reading (DocumentFactory) - separate from transformation
-
Transformation (Transformer) - separate from I/O
-
Writing (DocumentWriter) - separate from transformation
Provides explicit, declarative API with no magic or automatic detection.
Defined Under Namespace
Classes: ConversionResult
Instance Method Summary collapse
-
#batch_convert(sources:, source_format:, target_format:, target_dir:) ⇒ Array<ConversionResult>
Batch convert multiple files explicitly.
-
#convert(source:, source_format:, target:, target_format:, **_options) ⇒ ConversionResult
Convert between formats with explicit specification.
-
#docx_to_mhtml(source:, target:) ⇒ ConversionResult
Convert DOCX to MHTML with explicit declaration.
-
#initialize(options = {}) ⇒ FormatConverter
constructor
Initialize a new format converter.
-
#mhtml_to_docx(source:, target:) ⇒ ConversionResult
Convert MHTML to DOCX with explicit declaration.
Constructor Details
#initialize(options = {}) ⇒ FormatConverter
Initialize a new format converter
40 41 42 43 44 |
# File 'lib/uniword/format_converter.rb', line 40 def initialize( = {}) @logger = [:logger] @preserve_metadata = .fetch(:preserve_metadata, true) @transformer = [:transformer] || Transformation::Transformer.new end |
Instance Method Details
#batch_convert(sources:, source_format:, target_format:, target_dir:) ⇒ Array<ConversionResult>
Batch convert multiple files explicitly.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/uniword/format_converter.rb', line 175 def batch_convert(sources:, source_format:, target_format:, target_dir:) # Ensure target directory exists FileUtils.mkdir_p(target_dir) sources.map do |source| basename = File.basename(source, File.extname(source)) target = File.join(target_dir, "#{basename}.#{target_format}") convert( source: source, source_format: source_format, target: target, target_format: target_format ) end end |
#convert(source:, source_format:, target:, target_format:, **_options) ⇒ ConversionResult
Convert between formats with explicit specification.
All parameters must be explicitly specified - no automatic detection.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/uniword/format_converter.rb', line 68 def convert(source:, source_format:, target:, target_format:, **) validate_conversion_params(source, source_format, target, target_format) log_conversion_start(source, source_format, target, target_format) begin # Step 1: Read source file to model (deserialization) source_document = read_document(source, source_format) # Step 2: Transform model (model-to-model transformation) target_document = @transformer.transform( source: source_document, source_format: source_format, target_format: target_format ) # Step 3: Write target model to file (serialization) write_document(target_document, target, target_format) # Create success result ConversionResult.new( source: source, source_format: source_format, target: target, target_format: target_format, success: true, paragraphs_count: document_stats(target_document)[:paragraphs], tables_count: document_stats(target_document)[:tables], images_count: document_stats(target_document)[:images] ) rescue StandardError => e # Create failure result ConversionResult.new( source: source, source_format: source_format, target: target, target_format: target_format, success: false, error: e. ) end end |
#docx_to_mhtml(source:, target:) ⇒ ConversionResult
Convert DOCX to MHTML with explicit declaration.
Declarative method that clearly states the conversion operation.
149 150 151 152 153 154 155 156 157 |
# File 'lib/uniword/format_converter.rb', line 149 def docx_to_mhtml(source:, target:, **) convert( source: source, source_format: :docx, target: target, target_format: :mhtml, ** ) end |
#mhtml_to_docx(source:, target:) ⇒ ConversionResult
Convert MHTML to DOCX with explicit declaration.
Declarative method that clearly states the conversion operation.
125 126 127 128 129 130 131 132 133 |
# File 'lib/uniword/format_converter.rb', line 125 def mhtml_to_docx(source:, target:, **) convert( source: source, source_format: :mhtml, target: target, target_format: :docx, ** ) end |