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

Allow access to stubbed keys via the [] method. #15

Closed andrewcroome closed 8 years ago

andrewcroome commented 8 years ago

When using stubs, resolving :item via container.resolve(:item) returns the stub.

The small change included here ensures that container[:item] returns the stub as well.

timriley commented 8 years ago

I wonder if we could avoid having to add that extra alias here by changing our approach in Dry::Container::Mixin. Instead of using alias [] resolve there (alias is lexically scoped, which is why @andrewcroome has had to call it again inside the Stub module), we could just make #[] a proper method:

def [](key)
  resolve(key)
end

This should mean that the Stub mixing should work properly with #[] without the need for the additional alias, right?

andrewcroome commented 8 years ago

Yes, I think @timriley has the better way. I've amended this with that change.

timriley commented 8 years ago

@andrewcroome Fantastic, thank you!