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

Simple resolver customization causing errors #49

Closed TiteiKo closed 5 years ago

TiteiKo commented 5 years ago

Using the current code and documentation, a container having a custom resolver won't be able to use #keys, #each, #each_key or #key?, with the error that a Proc doesn't have those methods.

# current documentation for memory
class ContainerObject
  include Dry::Container::Mixin

  configure do |config|
    config.registry = ->(container, key, item, options) { container[key] = item }
    config.resolver = ->(container, key) { container[key] }
  end
end

The workaround I found is by subclassing Dry::Container::Resolver like this:

class ResolverObject < Dry::Container::Resolver
  def call(container, key)
    container[key]
  end
end

class ContainerObject
  include Dry::Container::Mixin

  configure do |config|
    config.registry = ->(container, key, item, options) { container[key] = item }
    config.resolver = ResolverObject.new
  end
end

But I don't understand the benefit of having those four methods delegated to the resolver as they are methods of the container?

Relevant lines of code concerned:

flash-gordon commented 5 years ago

I updated the docs, now it explicitly suggests subclassing instead of using a lambda https://dry-rb.org/gems/dry-container/registry-and-resolver/#customization I'll close this for now but we may revisit the resolver interface in future.