I’ve got a pretty simple setup like this:
class App < Dry::Component::Container ROOT = Pathname.new(__FILE__).dirname.dirname load_paths! 'app' configure do |config| config.root = ROOT config.auto_register = ['app/controllers', 'app/queries', 'app/views'] end end
It’s working fine, but I noticed that
App['views.sign_in'] is instantiating a new object every time it’s called. I assumed that it would only instantiate one object, and memoize it. The objects I’m working with are immutable and some are expensive to instantiate (e.g. template loading and precompilation), so I really only want to instantiate one of them.
I know I can do
App.register('views.sign_in', Views::SignIn.new), but is there a way do the same thing through auto-registration? It would be nice if it was lazy, so I can run a single test without booting up everything.