dry-rb / dry-rb/dry-container

Add ability to iterate over items registered in a namespace

Open
#68 8 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
Ruby
Stars
339
Forks
41
PR merge metrics
No merged PRs in 30d

Description

I have a use-case where I have several "parser" objects registered in a namespace within a container, and I'd like to iterate over each of those objects to find the first that works.

## Examples

I have a container like this:

```ruby
class MyContainer
extend Dry::Container::Mixin

namespace :parsers do
register(:first) { MyParser.new }
register(:another) { MyOtherParser.new }
register(:fallback) { MyFallbackParser.new }
end

namespace :more_things do
# ...
end
end
```

In my code, I want to try each one till I get one that works:

```ruby
MyContainer
.namespaced(:parsers) # Something like this
.lazy
.map { |parser| parser.call }
.detect(&:success?)
.presence || Failure("no valid parser found")
```

I can work around it by doing:

```ruby
MyContainer
.each
.select { |k,_| k.start_with? "parsers." }
.map(&:last)
```

... but that seems more cumbersome than it needs to be.

## Resources

@solnic asked my to open a feature request from [this convo](https://dry-rb.zulipchat.com/#narrow/stream/191662-general/topic/dry-container/near/187440289) in zulip.

Contributor guide

Open the contributing guide

Research direction

Start by reading the container's existing namespace, register, namespaced, and each APIs to understand how registered keys and values are exposed. Use the parsers example and current each/select workaround as the expected behavior: callers should be able to iterate only the items registered under one namespace and consume their resolved values. Check the existing test coverage around namespaces and iteration before deciding where the behavior belongs.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.