Class
MTLInMemoryStrategy
In-memory generation strategy for testing and programmatic access.
actor MTLInMemoryStrategy
Overview
This strategy accumulates generated text in memory rather than writing to files, making it ideal for unit tests, previews, and scenarios where the generated text needs to be processed programmatically.
Overview
Features:
-
No file system access: All output stored in memory
-
File simulation: Maintains separate buffers per “file”
-
Mode handling: Simulates append mode by concatenating to existing buffer
-
Retrieval: Generated files can be accessed via
getGeneratedFiles()
Example Usage
let strategy = MTLInMemoryStrategy()
// Generate multiple "files"
let writer1 = try await strategy.createWriter(
url: "file1.txt",
mode: .create,
charset: "UTF-8",
indentation: MTLIndentation()
)
await writer1.writeLine("Content 1")
try await strategy.finalizeWriter(writer1)
let writer2 = try await strategy.createWriter(
url: "file2.txt",
mode: .create,
charset: "UTF-8",
indentation: MTLIndentation()
)
await writer2.writeLine("Content 2")
try await strategy.finalizeWriter(writer2)
// Retrieve all generated content
let files = await strategy.getGeneratedFiles()
print(files["file1.txt"]) // "Content 1\n"
print(files["file2.txt"]) // "Content 2\n"
Topics
Initializers
Creates a new in-memory generation strategy.
Instance Methods
Clears all generated files from memory.
Creates a new writer for the specified target.
Finalizes and commits the writer’s content to its target.
Returns all generated files and their contents.