Class: Uniword::DocumentWriter
- Inherits:
-
Object
- Object
- Uniword::DocumentWriter
- 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.
Instance Attribute Summary collapse
-
#document ⇒ Wordprocessingml::DocumentRoot
readonly
The document to write.
Instance Method Summary collapse
-
#infer_format(path) ⇒ Symbol
Infer the format from file extension.
-
#initialize(document) ⇒ DocumentWriter
constructor
Initialize a new DocumentWriter.
-
#save(path, format: :auto, profile: nil) ⇒ void
Save the document to a file.
-
#write_to_stream(stream, format: :docx) ⇒ void
Write the document to a stream (StringIO).
Constructor Details
#initialize(document) ⇒ DocumentWriter
Initialize a new DocumentWriter.
27 28 29 30 |
# File 'lib/uniword/document_writer.rb', line 27 def initialize(document) validate_document(document) @document = document end |
Instance Attribute Details
#document ⇒ Wordprocessingml::DocumentRoot (readonly)
Returns The document to write.
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.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/uniword/document_writer.rb', line 72 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) ⇒ void
This method returns an undefined value.
Save the document to a file.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/uniword/document_writer.rb', line 45 def save(path, format: :auto, profile: nil) validate_path(path) format = infer_format(path) if format == :auto case format when :docx, :docm Docx::Package.to_file(document, path, profile: profile) when :dotx, :dotm Ooxml::DotxPackage.to_file(document, path, profile: profile) 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) ⇒ void
This method returns an undefined value.
Write the document to a stream (StringIO). Compatible with docx gem API
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/uniword/document_writer.rb', line 109 def write_to_stream(stream, format: :docx) 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) # 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 |