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

set default memoization for container #45

Open mbriggs opened 6 years ago

mbriggs commented 6 years ago

Looking for a way to memoize by default, since for my usage, not memoizing is more of a corner case.

Thanks :)

ushis commented 6 years ago

@mbriggs I think you can subclass Dry::Container::Registry and override #call, like:

MemoByDefaultRegistry < Dry::Container::Registry
  def call(container, key, item, options)
    super(container, key, item, {memoize: true}.merge(options))
  end
end

Dry::Container.configure do |config|
  config.registry = MemoByDefaultRegistry.new
end