dry-rb / dry-container

A simple, configurable object container implemented in Ruby
https://dry-rb.org/gems/dry-container
MIT License
335 stars 41 forks source link

Memoize decorate call appropriately #56

Closed endash closed 5 years ago

endash commented 5 years ago

I discovered this when working on specs for dry-system.

There are two issues IMO:

(1) #decorate turns a non-memoized item into, effectively, a memoized one. (2) The original item block is called immediately, when normally it would be called at resolve time. This difference in timing could introduce subtle bugs.

The added specs cover both problems.

Edit:

I've pushed a second commit which elaborates on this fix by generalizing #decorate, having it take a block instead of a class (or a stub, I guess, in the second path.) The user can then do anything with the original value in that block to decorate it. More flexible.

Add'l edit:

On second thought, original fix first, then maybe feature improvement.