Class: Uniword::DocumentWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/document_writer.rb

Overview

Writer for saving Document instances to files.

Responsibility: Handle document persistence operations. Follows Single Responsibility Principle - writing logic separated from Document class itself.

Examples:

Save document

writer = Uniword::DocumentWriter.new(document)
writer.save("output.docx")

Save with specific format

writer.save("output.mhtml", format: :mhtml)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ DocumentWriter

Initialize a new DocumentWriter.

Examples:

Create writer

writer = DocumentWriter.new(document)

Parameters:

Raises:

  • (ArgumentError)

    if document is invalid



27
28
29
30
# File 'lib/uniword/document_writer.rb', line 27

def initialize(document)
  validate_document(document)
  @document = document
end

Instance Attribute Details

#documentWordprocessingml::DocumentRoot (readonly)

Returns The document to write.

Returns:



18
19
20
# File 'lib/uniword/document_writer.rb', line 18

def document
  @document
end

Instance Method Details

#infer_format(path) ⇒ Symbol

Infer the format from file extension.

Examples:

Infer format

format = writer.infer_format("output.docx")
# => :docx

Parameters:

  • path (String)

    The file path

Returns:

  • (Symbol)

    The inferred format (:docx, :mhtml)

Raises:

  • (ArgumentError)

    if format cannot be inferred



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/uniword/document_writer.rb', line 81

def infer_format(path)
  extension = File.extname(path).downcase

  case extension
  when ".docx"
    :docx
  when ".docm"
    :docm
  when ".dotx"
    :dotx
  when ".dotm"
    :dotm
  when ".mhtml", ".mht"
    :mhtml
  when ".doc"
    # .doc can be MHTML saved with Word (not binary old Word format)
    # We don't support binary .doc output, but MHTML .doc is valid
    :mhtml
  else
    raise ArgumentError,
          "Cannot infer format from extension: #{extension}. " \
          "Supported extensions: .docx, .docm, .dotx, .dotm, .mhtml, .mht"
  end
end

#save(path, format: :auto, profile: nil, validate: nil) ⇒ void

This method returns an undefined value.

Save the document to a file.

Examples:

Save as DOCX

writer.save("output.docx")

Save with explicit format

writer.save("output.mht", format: :mhtml)

Parameters:

  • path (String)

    The output file path

  • format (Symbol) (defaults to: :auto)

    The format (:auto, :docx, :mhtml)

  • profile (Docx::Profile, nil) (defaults to: nil)

    Profile for reconciliation

  • validate (Boolean, nil) (defaults to: nil)

    Run the package integrity gate before writing; nil falls back to Uniword.configuration.validate_on_save. Not applicable to :mhtml (MIME, not an OPC/ZIP package — there are no content types, relationships parts, or ZIP entries to check).

Raises:

  • (ArgumentError)

    if path is invalid

  • (ArgumentError)

    if format is not supported

  • (Uniword::ValidationError)

    when the gate is enabled and the generated package content is invalid



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/uniword/document_writer.rb', line 52

def save(path, format: :auto, profile: nil, validate: nil)
  validate_path(path)

  format = infer_format(path) if format == :auto

  case format
  when :docx, :docm
    Docx::Package.to_file(document, path, profile: profile,
                                        validate: validate)
  when :dotx, :dotm
    Ooxml::DotxPackage.to_file(document, path, profile: profile,
                                             validate: validate)
  when :mhtml
    Mhtml::MhtmlPackage.to_file(document, path)
  else
    raise ArgumentError,
          "No handler registered for format: #{format.inspect}"
  end
end

#write_to_stream(stream, format: :docx, validate: nil) ⇒ void

This method returns an undefined value.

Write the document to a stream (StringIO). Compatible with docx gem API

Examples:

Write to StringIO

io = StringIO.new
writer.write_to_stream(io)
io.rewind
content = io.read

Parameters:

  • stream (IO, StringIO)

    The output stream

  • format (Symbol) (defaults to: :docx)

    The format (:docx, :mhtml)

  • validate (Boolean, nil) (defaults to: nil)

    Run the package integrity gate before writing; nil falls back to Uniword.configuration.validate_on_save



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/uniword/document_writer.rb', line 120

def write_to_stream(stream, format: :docx, validate: nil)
  require "tempfile"

  # Use a temporary file to generate the document
  temp_file = Tempfile.new(["uniword_stream", ".#{format}"], binmode: true)
  begin
    # Save to temp file
    save(temp_file.path, format: format, validate: validate)

    # Read and write to stream in binary mode
    temp_file.rewind
    content = File.binread(temp_file.path)
    stream.write(content)
  ensure
    temp_file.close
    temp_file.unlink
  end
end