dry-container 0.7.2 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +79 -39
- data/LICENSE +1 -1
- data/README.md +18 -12
- data/dry-container.gemspec +34 -22
- data/lib/dry/container/error.rb +7 -0
- data/lib/dry/container/item/callable.rb +3 -1
- data/lib/dry/container/item/factory.rb +4 -2
- data/lib/dry/container/item/memoizable.rb +4 -4
- data/lib/dry/container/item.rb +2 -0
- data/lib/dry/container/mixin.rb +74 -21
- data/lib/dry/container/namespace.rb +3 -0
- data/lib/dry/container/namespace_dsl.rb +3 -1
- data/lib/dry/container/registry.rb +3 -1
- data/lib/dry/container/resolver.rb +9 -6
- data/lib/dry/container/stub.rb +3 -1
- data/lib/dry/container/version.rb +3 -1
- data/lib/dry/container.rb +9 -8
- data/lib/dry-container.rb +3 -1
- metadata +24 -59
- data/.codeclimate.yml +0 -32
- data/.gitignore +0 -10
- data/.rspec +0 -2
- data/.rubocop.yml +0 -24
- data/.rubocop_todo.yml +0 -6
- data/.travis.yml +0 -30
- data/CONTRIBUTING.md +0 -29
- data/Gemfile +0 -19
- data/Rakefile +0 -12
- data/rakelib/rubocop.rake +0 -18
- data/spec/integration/container_spec.rb +0 -18
- data/spec/integration/mixin_spec.rb +0 -32
- data/spec/spec_helper.rb +0 -104
- data/spec/support/shared_examples/container.rb +0 -664
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1142596b0e507d66a8d2fc50b0e547e562194002a66b560945b386007fee17ae
|
4
|
+
data.tar.gz: 01a300ee230fa9047f002d90c0a02fe8d80da49e56706deb2a29da67d2e1ea08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7df2087f89345b9e5225f24226b7b20a2beceec88ebd424fa4a759808f7df1b355d2771370b4fbdc26d536891a68bd0c1f5b9353cb2301d10c80787466f59eaa
|
7
|
+
data.tar.gz: 3d83f9c597b41ad3b446ee0f1441ed42b443ac70ad0775bd17d8d5947fdf2508bf0f883e5f298ec16e908cd07bb7966f3688fba9ccd5606d76c400a76d0822c9
|
data/CHANGELOG.md
CHANGED
@@ -1,49 +1,75 @@
|
|
1
|
-
|
1
|
+
<!--- DO NOT EDIT THIS FILE - IT'S AUTOMATICALLY GENERATED VIA DEVTOOLS --->
|
2
2
|
|
3
|
-
##
|
3
|
+
## 0.10.0 2022-07-10
|
4
4
|
|
5
|
-
|
5
|
+
⚠️ Notice that dry-container no longer depends on dry-configurable. If you happen to rely on the advanced configurable functionality, add dry-configurable as a dependency and require it **before** requiring dry-container.
|
6
|
+
|
7
|
+
### Added
|
8
|
+
|
9
|
+
- Accept block given to `#merge` (via #83) (@timriley)
|
10
|
+
|
11
|
+
### Changed
|
12
|
+
|
13
|
+
- [BREAKING] Replace dep on dry-configurable with a simple `Configuration` mod (see #84 for more details) (@solnic)
|
14
|
+
- Raise KeyError for missing key, with DidYouMean integration (via #82) (@cllns)
|
15
|
+
- Wrap FrozenError, to state that the container itself is frozen (see #74) (@cllns)
|
16
|
+
|
17
|
+
[Compare v0.9.0...v0.10.0](https://github.com/dry-rb/dry-container/compare/v0.9.0...v0.10.0)
|
18
|
+
|
19
|
+
## 0.9.0 2021-09-12
|
20
|
+
|
21
|
+
|
22
|
+
### Changed
|
23
|
+
|
24
|
+
- [internal] Upgraded to new `setting` API provided in dry-configurable 0.13.0 (@timriley in #77)
|
25
|
+
|
26
|
+
[Compare v0.8.0...v0.9.0](https://github.com/dry-rb/dry-container/compare/v0.8.0...v0.9.0)
|
27
|
+
|
28
|
+
## 0.8.0 2021-06-06
|
29
|
+
|
30
|
+
|
31
|
+
### Changed
|
32
|
+
|
33
|
+
- [BREAKING] Support for 2.4 and 2.5 was dropped
|
34
|
+
|
35
|
+
[Compare v0.7.2...v0.8.0](https://github.com/dry-rb/dry-container/compare/v0.7.2...v0.8.0)
|
36
|
+
|
37
|
+
## 0.7.2 2019-07-09
|
38
|
+
|
39
|
+
|
40
|
+
### Added
|
41
|
+
|
42
|
+
- `.resolve` accepts an optional fallback block, similar to how `Hash#fetch` works ([flash-gordon](https://github.com/flash-gordon))
|
6
43
|
```ruby
|
7
44
|
container.resolve('missing_key') { :fallback } # => :fallback
|
8
45
|
```
|
9
|
-
|
46
|
+
- `.decorate` can (again) work with static values. Also, it can take a block instead of `with` ([flash-gordon](https://github.com/flash-gordon))
|
10
47
|
```ruby
|
11
48
|
container.register('key', 'value')
|
12
49
|
container.decorate('key') { |v| "<'#{v}'>" }
|
13
50
|
container.resolve('key') # => "<'value'>"
|
14
51
|
```
|
15
52
|
|
16
|
-
[Compare v0.7.1...0.7.2](https://github.com/dry-rb/dry-container/compare/v0.7.1...v0.7.2)
|
17
53
|
|
18
|
-
|
54
|
+
[Compare v0.7.1...v0.7.2](https://github.com/dry-rb/dry-container/compare/v0.7.1...v0.7.2)
|
19
55
|
|
20
|
-
##
|
56
|
+
## 0.7.1 2019-06-07
|
21
57
|
|
22
|
-
* Added `Mixin#dup` and `Mixin#clone`, now copies don't share underlying containers (flash-gordon)
|
23
58
|
|
24
|
-
|
59
|
+
### Fixed
|
25
60
|
|
26
|
-
|
61
|
+
- Added `Mixin#dup` and `Mixin#clone`, now copies don't share underlying containers (flash-gordon)
|
27
62
|
|
28
|
-
## Changed
|
29
63
|
|
30
|
-
|
64
|
+
[Compare v0.7.0...v0.7.1](https://github.com/dry-rb/dry-container/compare/v0.7.0...v0.7.1)
|
31
65
|
|
32
|
-
##
|
66
|
+
## 0.7.0 2019-02-05
|
33
67
|
|
34
|
-
* Symbols are now coerced to strings when resolving stubbed dependencies ([cthulhu666](https://github.com/cthulhu666))
|
35
|
-
* Stubbing keys not present in container will raise an error ([flash-gordon](https://github.com/flash-gordon))
|
36
68
|
|
37
|
-
|
38
|
-
```
|
39
|
-
ArgumentError (cannot stub "something" - no such key in container)
|
40
|
-
```
|
41
|
-
Be sure you register dependencies before using them. The new behavior will likely save quite a bit of time when debugging ill-configured container setups.
|
42
|
-
|
43
|
-
## Added
|
69
|
+
### Added
|
44
70
|
|
45
|
-
|
46
|
-
|
71
|
+
- Namespace DSL resolves keys relative to the current namespace, see the corresponding [changes](https://github.com/dry-rb/dry-container/pull/47) ([yuszuv](https://github.com/yuszuv))
|
72
|
+
- Registered objects can be decorated with the following API ([igor-alexandrov](https://github.com/igor-alexandrov))
|
47
73
|
|
48
74
|
```ruby
|
49
75
|
class CreateUser
|
@@ -59,34 +85,48 @@
|
|
59
85
|
container.resolve('create_user')
|
60
86
|
# => #<ShinyLogger @obj=#<CreateUser:0x...>]>
|
61
87
|
```
|
62
|
-
|
88
|
+
- Freezing a container now prevents further registrations ([flash-gordon](https://github.com/flash-gordon))
|
89
|
+
- ## Internal
|
90
|
+
- Handling container items was generalized in [#34](https://github.com/dry-rb/dry-container/pull/34) ([GabrielMalakias](https://github.com/GabrielMalakias))
|
91
|
+
|
92
|
+
### Fixed
|
93
|
+
|
94
|
+
- Symbols are now coerced to strings when resolving stubbed dependencies ([cthulhu666](https://github.com/cthulhu666))
|
95
|
+
- Stubbing keys not present in container will raise an error ([flash-gordon](https://github.com/flash-gordon))
|
96
|
+
|
97
|
+
This means after upgrading you may see errors like this
|
98
|
+
```
|
99
|
+
ArgumentError (cannot stub "something" - no such key in container)
|
100
|
+
```
|
101
|
+
Be sure you register dependencies before using them. The new behavior will likely save quite a bit of time when debugging ill-configured container setups.
|
102
|
+
|
103
|
+
### Changed
|
104
|
+
|
105
|
+
- [BREAKING] Now only Ruby 2.3 and above is supported ([flash-gordon](https://github.com/flash-gordon))
|
63
106
|
|
64
|
-
|
107
|
+
[Compare v0.6.0...v0.7.0](https://github.com/dry-rb/dry-container/compare/v0.6.0...v0.7.0)
|
65
108
|
|
66
|
-
|
109
|
+
## 0.6.0 2016-12-09
|
67
110
|
|
68
|
-
[Compare v0.6.0...0.7.0](https://github.com/dry-rb/dry-container/compare/v0.6.0...v0.7.0)
|
69
111
|
|
70
|
-
|
112
|
+
### Added
|
71
113
|
|
72
|
-
|
114
|
+
- `Dry::Container::Mixin#each` - provides a means of seeing what all is registered in the container ([jeremyf](https://github.com/jeremyf))
|
73
115
|
|
74
|
-
|
116
|
+
### Fixed
|
75
117
|
|
76
|
-
|
118
|
+
- Including mixin into a class with a custom initializer ([maltoe](https://github.com/maltoe))
|
77
119
|
|
78
|
-
* Including mixin into a class with a custom initializer ([maltoe](https://github.com/maltoe))
|
79
120
|
|
80
121
|
[Compare v0.5.0...v0.6.0](https://github.com/dry-rb/dry-container/compare/v0.5.0...v0.6.0)
|
81
122
|
|
82
|
-
##
|
123
|
+
## 0.5.0 2016-08-31
|
83
124
|
|
84
|
-
## Added
|
85
125
|
|
86
|
-
|
126
|
+
### Added
|
87
127
|
|
88
|
-
|
128
|
+
- `memoize` option to `#register` - memoizes items on first resolve ([ivoanjo](https://github.com/ivoanjo))
|
89
129
|
|
90
|
-
|
130
|
+
### Fixed
|
91
131
|
|
92
|
-
|
132
|
+
- `required_ruby_version` set to `>= 2.0.0` ([artofhuman](https://github.com/artofhuman))
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,23 +1,29 @@
|
|
1
|
+
<!--- this file is synced from dry-rb/template-gem project -->
|
1
2
|
[gem]: https://rubygems.org/gems/dry-container
|
2
|
-
[
|
3
|
-
[
|
4
|
-
[test_coverage]: https://codeclimate.com/github/dry-rb/dry-container/test_coverage
|
5
|
-
[inch]: http://inch-ci.org/github/dry-rb/dry-container
|
3
|
+
[actions]: https://github.com/dry-rb/dry-container/actions
|
4
|
+
[codacy]: https://www.codacy.com/gh/dry-rb/dry-container
|
6
5
|
[chat]: https://dry-rb.zulipchat.com
|
6
|
+
[inchpages]: http://inch-ci.org/github/dry-rb/dry-container
|
7
7
|
|
8
8
|
# dry-container [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
|
9
9
|
|
10
|
-
[![Gem Version](https://
|
11
|
-
[![
|
12
|
-
[![
|
13
|
-
[![
|
14
|
-
[![
|
15
|
-
|
16
|
-
A simple, configurable container implemented in Ruby.
|
10
|
+
[![Gem Version](https://badge.fury.io/rb/dry-container.svg)][gem]
|
11
|
+
[![CI Status](https://github.com/dry-rb/dry-container/workflows/ci/badge.svg)][actions]
|
12
|
+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/227509c9034340b493f769f6277f4ecb)][codacy]
|
13
|
+
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/227509c9034340b493f769f6277f4ecb)][codacy]
|
14
|
+
[![Inline docs](http://inch-ci.org/github/dry-rb/dry-container.svg?branch=main)][inchpages]
|
17
15
|
|
18
16
|
## Links
|
19
17
|
|
20
|
-
[
|
18
|
+
* [User documentation](https://dry-rb.org/gems/dry-container)
|
19
|
+
* [API documentation](http://rubydoc.info/gems/dry-container)
|
20
|
+
|
21
|
+
## Supported Ruby versions
|
22
|
+
|
23
|
+
This library officially supports the following Ruby versions:
|
24
|
+
|
25
|
+
* MRI `>= 2.7.0`
|
26
|
+
* jruby `>= 9.3` (postponed until 2.7 is supported)
|
21
27
|
|
22
28
|
## License
|
23
29
|
|
data/dry-container.gemspec
CHANGED
@@ -1,25 +1,37 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# this file is synced from dry-rb/template-gem project
|
4
|
+
|
5
|
+
lib = File.expand_path("lib", __dir__)
|
6
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
7
|
+
require "dry/container/version"
|
2
8
|
|
3
9
|
Gem::Specification.new do |spec|
|
4
|
-
spec.name =
|
5
|
-
spec.
|
6
|
-
spec.
|
7
|
-
spec.
|
8
|
-
spec.
|
9
|
-
|
10
|
-
spec.
|
11
|
-
|
12
|
-
spec.
|
13
|
-
spec.
|
14
|
-
spec.
|
15
|
-
spec.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
spec.
|
20
|
-
spec.
|
21
|
-
|
22
|
-
|
23
|
-
spec.
|
24
|
-
|
10
|
+
spec.name = "dry-container"
|
11
|
+
spec.authors = ["Andy Holland"]
|
12
|
+
spec.email = ["andyholland1991@aol.com"]
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.version = Dry::Container::VERSION.dup
|
15
|
+
|
16
|
+
spec.summary = "A simple, configurable object container implemented in Ruby"
|
17
|
+
spec.description = spec.summary
|
18
|
+
spec.homepage = "https://dry-rb.org/gems/dry-container"
|
19
|
+
spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-container.gemspec", "lib/**/*"]
|
20
|
+
spec.bindir = "bin"
|
21
|
+
spec.executables = []
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
25
|
+
spec.metadata["changelog_uri"] = "https://github.com/dry-rb/dry-container/blob/master/CHANGELOG.md"
|
26
|
+
spec.metadata["source_code_uri"] = "https://github.com/dry-rb/dry-container"
|
27
|
+
spec.metadata["bug_tracker_uri"] = "https://github.com/dry-rb/dry-container/issues"
|
28
|
+
|
29
|
+
spec.required_ruby_version = ">= 2.7.0"
|
30
|
+
|
31
|
+
# to update dependencies edit project.yml
|
32
|
+
spec.add_runtime_dependency "concurrent-ruby", "~> 1.0"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler"
|
35
|
+
spec.add_development_dependency "rake"
|
36
|
+
spec.add_development_dependency "rspec"
|
25
37
|
end
|
data/lib/dry/container/error.rb
CHANGED
@@ -1,6 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Dry
|
2
4
|
class Container
|
3
5
|
# @api public
|
4
6
|
Error = Class.new(StandardError)
|
7
|
+
|
8
|
+
KeyError = Class.new(::KeyError)
|
9
|
+
DidYouMean.correct_error(KeyError, DidYouMean::KeyErrorChecker)
|
10
|
+
|
11
|
+
deprecate_constant(:Error)
|
5
12
|
end
|
6
13
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry/container/item"
|
2
4
|
|
3
5
|
module Dry
|
4
6
|
class Container
|
@@ -39,11 +41,9 @@ module Dry
|
|
39
41
|
|
40
42
|
# @private
|
41
43
|
def raise_not_supported_error
|
42
|
-
raise ::Dry::Container::Error,
|
44
|
+
raise ::Dry::Container::Error, "Memoize only supported for a block or a proc"
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|
48
|
-
|
49
|
-
|
data/lib/dry/container/item.rb
CHANGED
data/lib/dry/container/mixin.rb
CHANGED
@@ -1,8 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
|
-
require
|
3
|
+
require "concurrent/hash"
|
3
4
|
|
4
5
|
module Dry
|
5
6
|
class Container
|
7
|
+
# @api public
|
8
|
+
class Config
|
9
|
+
DEFAULT_NAMESPACE_SEPARATOR = "."
|
10
|
+
DEFAULT_RESOLVER = Resolver.new
|
11
|
+
DEFAULT_REGISTRY = Registry.new
|
12
|
+
|
13
|
+
# @api public
|
14
|
+
attr_accessor :namespace_separator
|
15
|
+
|
16
|
+
# @api public
|
17
|
+
attr_accessor :resolver
|
18
|
+
|
19
|
+
# @api public
|
20
|
+
attr_accessor :registry
|
21
|
+
|
22
|
+
# @api private
|
23
|
+
def initialize(
|
24
|
+
namespace_separator: DEFAULT_NAMESPACE_SEPARATOR,
|
25
|
+
resolver: DEFAULT_RESOLVER,
|
26
|
+
registry: DEFAULT_REGISTRY
|
27
|
+
)
|
28
|
+
@namespace_separator = namespace_separator
|
29
|
+
@resolver = resolver
|
30
|
+
@registry = registry
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# @api public
|
35
|
+
module Configuration
|
36
|
+
# Use dry/configurable if it's available
|
37
|
+
if defined?(Configurable)
|
38
|
+
# @api private
|
39
|
+
def self.extended(klass)
|
40
|
+
super
|
41
|
+
klass.class_eval do
|
42
|
+
extend Dry::Configurable
|
43
|
+
|
44
|
+
setting :namespace_separator, default: Config::DEFAULT_NAMESPACE_SEPARATOR
|
45
|
+
setting :resolver, default: Config::DEFAULT_RESOLVER
|
46
|
+
setting :registry, default: Config::DEFAULT_REGISTRY
|
47
|
+
end
|
48
|
+
end
|
49
|
+
else
|
50
|
+
# @api private
|
51
|
+
def config
|
52
|
+
@config ||= Config.new
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# @api private
|
57
|
+
def configure
|
58
|
+
yield config
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
6
62
|
PREFIX_NAMESPACE = lambda do |namespace, key, config|
|
7
63
|
[namespace, key].join(config.namespace_separator)
|
8
64
|
end
|
@@ -30,7 +86,6 @@ module Dry
|
|
30
86
|
# container.resolve(:item)
|
31
87
|
# => 'item'
|
32
88
|
#
|
33
|
-
#
|
34
89
|
# @api public
|
35
90
|
module Mixin
|
36
91
|
# @private
|
@@ -43,13 +98,9 @@ module Dry
|
|
43
98
|
end
|
44
99
|
|
45
100
|
base.class_eval do
|
46
|
-
extend
|
101
|
+
extend Configuration
|
47
102
|
extend hooks_mod
|
48
103
|
|
49
|
-
setting :registry, ::Dry::Container::Registry.new
|
50
|
-
setting :resolver, ::Dry::Container::Resolver.new
|
51
|
-
setting :namespace_separator, '.'
|
52
|
-
|
53
104
|
@_container = ::Concurrent::Hash.new
|
54
105
|
end
|
55
106
|
end
|
@@ -65,13 +116,9 @@ module Dry
|
|
65
116
|
# @private
|
66
117
|
def self.included(base)
|
67
118
|
base.class_eval do
|
68
|
-
extend
|
119
|
+
extend Configuration
|
69
120
|
prepend Initializer
|
70
121
|
|
71
|
-
setting :registry, ::Dry::Container::Registry.new
|
72
|
-
setting :resolver, ::Dry::Container::Resolver.new
|
73
|
-
setting :namespace_separator, '.'
|
74
|
-
|
75
122
|
def config
|
76
123
|
self.class.config
|
77
124
|
end
|
@@ -104,6 +151,9 @@ module Dry
|
|
104
151
|
config.registry.call(_container, key, item, options)
|
105
152
|
|
106
153
|
self
|
154
|
+
rescue FrozenError
|
155
|
+
raise FrozenError,
|
156
|
+
"can't modify frozen #{self.class} (when attempting to register '#{key}')"
|
107
157
|
end
|
108
158
|
|
109
159
|
# Resolve an item from the container
|
@@ -144,15 +194,16 @@ module Dry
|
|
144
194
|
# @return [Dry::Container::Mixin] self
|
145
195
|
#
|
146
196
|
# @api public
|
147
|
-
def merge(other, namespace: nil)
|
197
|
+
def merge(other, namespace: nil, &block)
|
148
198
|
if namespace
|
149
199
|
_container.merge!(
|
150
|
-
other._container.each_with_object(::Concurrent::Hash.new)
|
151
|
-
|
152
|
-
|
200
|
+
other._container.each_with_object(::Concurrent::Hash.new) { |(key, item), hsh|
|
201
|
+
hsh[PREFIX_NAMESPACE.call(namespace, key, config)] = item
|
202
|
+
},
|
203
|
+
&block
|
153
204
|
)
|
154
205
|
else
|
155
|
-
_container.merge!(other._container)
|
206
|
+
_container.merge!(other._container, &block)
|
156
207
|
end
|
157
208
|
|
158
209
|
self
|
@@ -191,7 +242,8 @@ module Dry
|
|
191
242
|
self
|
192
243
|
end
|
193
244
|
|
194
|
-
# Calls block once for each key/value pair in the container, passing the key and
|
245
|
+
# Calls block once for each key/value pair in the container, passing the key and
|
246
|
+
# the registered item parameters.
|
195
247
|
#
|
196
248
|
# If no block is given, an enumerator is returned instead.
|
197
249
|
#
|
@@ -199,9 +251,9 @@ module Dry
|
|
199
251
|
#
|
200
252
|
# @api public
|
201
253
|
#
|
202
|
-
# @note In discussions with other developers, it was felt that being able to iterate
|
203
|
-
# the registered keys, but to see what was registered would be
|
204
|
-
# toward doing that.
|
254
|
+
# @note In discussions with other developers, it was felt that being able to iterate
|
255
|
+
# over not just the registered keys, but to see what was registered would be
|
256
|
+
# very helpful. This is a step toward doing that.
|
205
257
|
def each(&block)
|
206
258
|
config.resolver.each(_container, &block)
|
207
259
|
end
|
@@ -292,5 +344,6 @@ module Dry
|
|
292
344
|
copy
|
293
345
|
end
|
294
346
|
end
|
347
|
+
# rubocop: enable Metrics/ModuleLength
|
295
348
|
end
|
296
349
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Dry
|
2
4
|
class Container
|
3
5
|
# Create a namespace to be imported
|
@@ -22,6 +24,7 @@ module Dry
|
|
22
24
|
attr_reader :name
|
23
25
|
# @return [Proc] The block to be executed when the namespace is imported
|
24
26
|
attr_reader :block
|
27
|
+
|
25
28
|
# Create a new namespace
|
26
29
|
#
|
27
30
|
# @param [Mixed] name
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Dry
|
2
4
|
class Container
|
3
5
|
# Default resolver for resolving items from container
|
@@ -14,7 +16,7 @@ module Dry
|
|
14
16
|
# Fallback block to call when a key is missing. Its result will be returned
|
15
17
|
# @yieldparam [Mixed] key Missing key
|
16
18
|
#
|
17
|
-
# @raise [
|
19
|
+
# @raise [KeyError]
|
18
20
|
# If the given key is not registered with the container (and no block provided)
|
19
21
|
#
|
20
22
|
#
|
@@ -26,7 +28,7 @@ module Dry
|
|
26
28
|
if block_given?
|
27
29
|
return yield(key)
|
28
30
|
else
|
29
|
-
raise
|
31
|
+
raise KeyError.new(%(key not found: "#{key}"), key: key.to_s, receiver: container)
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
@@ -67,16 +69,17 @@ module Dry
|
|
67
69
|
container.each_key(&block)
|
68
70
|
end
|
69
71
|
|
70
|
-
# Calls block once for each key in container, passing the key and
|
72
|
+
# Calls block once for each key in container, passing the key and
|
73
|
+
# the registered item parameters.
|
71
74
|
#
|
72
75
|
# If no block is given, an enumerator is returned instead.
|
73
76
|
#
|
74
77
|
# @return Key, Value
|
75
78
|
#
|
76
79
|
# @api public
|
77
|
-
# @note In discussions with other developers, it was felt that being able
|
78
|
-
# the registered keys, but to see what was
|
79
|
-
# toward doing that.
|
80
|
+
# @note In discussions with other developers, it was felt that being able
|
81
|
+
# to iterate over not just the registered keys, but to see what was
|
82
|
+
# registered would be very helpful. This is a step toward doing that.
|
80
83
|
def each(container, &block)
|
81
84
|
container.map { |key, value| [key, value.call] }.each(&block)
|
82
85
|
end
|
data/lib/dry/container/stub.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Dry
|
2
4
|
class Container
|
3
5
|
module Stub
|
@@ -11,7 +13,7 @@ module Dry
|
|
11
13
|
# Add a stub to the container
|
12
14
|
def stub(key, value, &block)
|
13
15
|
unless key?(key)
|
14
|
-
raise ArgumentError, "cannot stub #{
|
16
|
+
raise ArgumentError, "cannot stub #{key.to_s.inspect} - no such key in container"
|
15
17
|
end
|
16
18
|
|
17
19
|
_stubs[key.to_s] = value
|
data/lib/dry/container.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry/container/error"
|
4
|
+
require "dry/container/namespace"
|
5
|
+
require "dry/container/registry"
|
6
|
+
require "dry/container/resolver"
|
7
|
+
require "dry/container/namespace_dsl"
|
8
|
+
require "dry/container/mixin"
|
9
|
+
require "dry/container/version"
|
9
10
|
|
10
11
|
# A collection of micro-libraries, each intended to encapsulate
|
11
12
|
# a common task in Ruby
|
data/lib/dry-container.rb
CHANGED