A small service prepares a bundle of shipping labels from a list of shipments. After a recent change, the results started depending on later actions of the calling code: some data in an already built bundle can unexpectedly change, and tags for different shipments sometimes begin to “share” the same state.
ShipmentLabelBundle.build(shipments) method accepts an array of shipment hashes.:id — shipment identifier;:recipient — recipient name;:tags — an array of string tags;:meta — a hash with additional fields.:labels — an array of prepared labels in the original order;:by_id — an index of labels by :id.shipments = [
{ id: "S-1", recipient: "Mila", tags: ["fragile"], meta: { zone: "A" } },
{ id: "S-2", recipient: "Oleg", tags: [], meta: { zone: "B" } }
]
bundle = ShipmentLabelBundle.build(shipments)
shipments[0][:tags] << "priority"
shipments[0][:meta][:zone] = "X"
Check the already prepared bundle[:labels][0].
Separately, check that adding a new tag to one label in the result does not change the other:
bundle[:labels][0][:tags] << "gift"
shipments after calling build must not change the already returned bundle.:tags and :meta, not shared with the input or with other labels.:by_id index must point to the same logical data as the elements in :labels, without hidden side effects between different shipments.3.3.0ruby, without Fiber schedulerruby shipment_label_bundle.rb{ id: String, recipient: String, tags: Array<String> | nil, meta: Hash | nil }id is unique within a single build callUse line comments to explain the problem, its impact, the fix and how to test it.