A small module builds an attachment index by document for the preview screen. After a recent PR, the result became unstable: documents without attachments start affecting each other, and the already obtained index can change after processing subsequent documents.
build method must accept an array of document hashes.:id and an optional :attachments array.id => { files: [...], total: Integer }.:attachments, an empty files list and total: 0 must be returned.build calls.files for one document in the returned result must not affect other documents.require_relative "attachment_index"
index = AttachmentIndex.new
first = index.build([
{ id: "doc-1" },
{ id: "doc-2", attachments: ["a.pdf", "a.pdf"] }
])
second = index.build([
{ id: "doc-3" }
])
p first
p second
first["doc-1"][:files] << "manual.txt"
p first
"doc-1" and "doc-3" must have independent empty files lists.build call must not change the already stored first result.files for "doc-1" must not add the same elements to other documents without attachments.ruby or irb from the directory with the filesAttachmentIndex#build(documents) -> Hash{String => { files: Array<String>, total: Integer }}Use line comments to explain the problem, its impact, the fix and how to test it.