dry-container 0.5.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +93 -6
- data/LICENSE +1 -1
- data/README.md +18 -158
- data/dry-container.gemspec +39 -23
- data/lib/dry-container.rb +3 -1
- data/lib/dry/container.rb +10 -10
- data/lib/dry/container/error.rb +3 -1
- data/lib/dry/container/item.rb +29 -22
- data/lib/dry/container/item/callable.rb +22 -0
- data/lib/dry/container/item/factory.rb +26 -0
- data/lib/dry/container/item/memoizable.rb +49 -0
- data/lib/dry/container/mixin.rb +90 -14
- data/lib/dry/container/namespace.rb +2 -0
- data/lib/dry/container/namespace_dsl.rb +8 -0
- data/lib/dry/container/registry.rb +9 -1
- data/lib/dry/container/resolver.rb +27 -4
- data/lib/dry/container/stub.rb +10 -3
- data/lib/dry/container/version.rb +3 -1
- metadata +16 -27
- data/.codeclimate.yml +0 -32
- data/.gitignore +0 -8
- data/.rspec +0 -3
- data/.rubocop.yml +0 -24
- data/.rubocop_todo.yml +0 -6
- data/.travis.yml +0 -31
- data/Gemfile +0 -15
- 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 -19
- data/spec/spec_helper.rb +0 -13
- data/spec/support/shared_examples/container.rb +0 -479
data/.codeclimate.yml
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
engines:
|
2
|
-
rubocop:
|
3
|
-
enabled: true
|
4
|
-
checks:
|
5
|
-
Rubocop/Metrics/LineLength:
|
6
|
-
enabled: true
|
7
|
-
max: 120
|
8
|
-
Rubocop/Style/Documentation:
|
9
|
-
enabled: false
|
10
|
-
Rubocop/Lint/HandleExceptions:
|
11
|
-
enabled: true
|
12
|
-
exclude:
|
13
|
-
- rakelib/*.rake
|
14
|
-
Rubocop/Lint/NestedMethodDefinition:
|
15
|
-
enabled: true
|
16
|
-
exclude:
|
17
|
-
- lib/dry/container/mixin.rb
|
18
|
-
Rubocop/Metrics/MethodLength:
|
19
|
-
enabled: true
|
20
|
-
exclude:
|
21
|
-
- lib/dry/container/mixin.rb
|
22
|
-
Rubocop/Style/FileName:
|
23
|
-
enabled: true
|
24
|
-
exclude:
|
25
|
-
- lib/dry-container.rb
|
26
|
-
|
27
|
-
ratings:
|
28
|
-
paths:
|
29
|
-
- lib/**/*.rb
|
30
|
-
exclude_paths:
|
31
|
-
- spec/**/*
|
32
|
-
- examples/**/*
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.rubocop.yml
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
# Generated by `rubocop --auto-gen-config`
|
2
|
-
inherit_from: .rubocop_todo.yml
|
3
|
-
|
4
|
-
Metrics/LineLength:
|
5
|
-
Max: 120
|
6
|
-
|
7
|
-
Style/Documentation:
|
8
|
-
Enabled: false
|
9
|
-
|
10
|
-
Lint/HandleExceptions:
|
11
|
-
Exclude:
|
12
|
-
- rakelib/*.rake
|
13
|
-
|
14
|
-
Lint/NestedMethodDefinition:
|
15
|
-
Exclude:
|
16
|
-
- lib/dry/container/mixin.rb
|
17
|
-
|
18
|
-
Metrics/MethodLength:
|
19
|
-
Exclude:
|
20
|
-
- lib/dry/container/mixin.rb
|
21
|
-
|
22
|
-
Style/FileName:
|
23
|
-
Exclude:
|
24
|
-
- lib/dry-container.rb
|
data/.rubocop_todo.yml
DELETED
@@ -1,6 +0,0 @@
|
|
1
|
-
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
-
# on 2015-06-16 19:51:19 +0100 using RuboCop version 0.32.0.
|
3
|
-
# The point is for the user to remove these configuration records
|
4
|
-
# one by one as the offenses are removed from the code base.
|
5
|
-
# Note that changes in the inspected code, or installation of new
|
6
|
-
# versions of RuboCop, may require this file to be generated again.
|
data/.travis.yml
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
sudo: false
|
3
|
-
cache: bundler
|
4
|
-
bundler_args: --without console
|
5
|
-
script:
|
6
|
-
- bundle exec rake spec
|
7
|
-
rvm:
|
8
|
-
- 2.0
|
9
|
-
- 2.1
|
10
|
-
- 2.2
|
11
|
-
- rbx-2
|
12
|
-
- ruby-head
|
13
|
-
- jruby-head
|
14
|
-
env:
|
15
|
-
global:
|
16
|
-
- JRUBY_OPTS='--dev -J-Xmx1024M'
|
17
|
-
matrix:
|
18
|
-
allow_failures:
|
19
|
-
- rvm: ruby-head
|
20
|
-
- rvm: jruby-head
|
21
|
-
include:
|
22
|
-
- rvm: jruby-9.0.0.0
|
23
|
-
before_install: rvm get master && rvm install jruby-9.0.0.0 && gem install bundler --no-ri --no-rdoc
|
24
|
-
notifications:
|
25
|
-
email: false
|
26
|
-
webhooks:
|
27
|
-
urls:
|
28
|
-
- https://webhooks.gitter.im/e/19098b4253a72c9796db
|
29
|
-
on_success: change # options: [always|never|change] default: always
|
30
|
-
on_failure: always # options: [always|never|change] default: always
|
31
|
-
on_start: false # default: false
|
data/Gemfile
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
gemspec
|
4
|
-
|
5
|
-
group :test do
|
6
|
-
gem 'codeclimate-test-reporter', platform: :rbx, require: false
|
7
|
-
end
|
8
|
-
|
9
|
-
group :tools do
|
10
|
-
gem 'rubocop'
|
11
|
-
gem 'guard'
|
12
|
-
gem 'guard-rspec'
|
13
|
-
gem 'guard-rubocop'
|
14
|
-
gem 'listen', '3.0.6'
|
15
|
-
end
|
data/Rakefile
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
#!/usr/bin/env rake
|
2
|
-
require 'bundler/gem_tasks'
|
3
|
-
|
4
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
5
|
-
|
6
|
-
require 'rspec/core'
|
7
|
-
require 'rspec/core/rake_task'
|
8
|
-
|
9
|
-
task default: :spec
|
10
|
-
|
11
|
-
desc 'Run all specs in spec directory'
|
12
|
-
RSpec::Core::RakeTask.new(:spec)
|
data/rakelib/rubocop.rake
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'rubocop/rake_task'
|
3
|
-
|
4
|
-
Rake::Task[:default].enhance [:rubocop]
|
5
|
-
|
6
|
-
RuboCop::RakeTask.new do |task|
|
7
|
-
task.options << '--display-cop-names'
|
8
|
-
end
|
9
|
-
|
10
|
-
namespace :rubocop do
|
11
|
-
desc 'Generate a configuration file acting as a TODO list.'
|
12
|
-
task :auto_gen_config do
|
13
|
-
exec 'bundle exec rubocop --auto-gen-config'
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
rescue LoadError
|
18
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
RSpec.describe Dry::Container do
|
2
|
-
let(:klass) { Dry::Container }
|
3
|
-
let(:container) { klass.new }
|
4
|
-
|
5
|
-
it_behaves_like 'a container'
|
6
|
-
|
7
|
-
describe 'inheritance' do
|
8
|
-
it 'sets up a container for a child class' do
|
9
|
-
parent = Class.new { extend Dry::Container::Mixin }
|
10
|
-
child = Class.new(parent)
|
11
|
-
|
12
|
-
parent.register(:foo, 'foo')
|
13
|
-
child.register(:foo, 'foo')
|
14
|
-
|
15
|
-
expect(parent[:foo]).to_not be(child[:foo])
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
RSpec.describe Dry::Container::Mixin do
|
2
|
-
describe 'extended' do
|
3
|
-
let(:klass) do
|
4
|
-
Class.new { extend Dry::Container::Mixin }
|
5
|
-
end
|
6
|
-
let(:container) { klass }
|
7
|
-
|
8
|
-
it_behaves_like 'a container'
|
9
|
-
end
|
10
|
-
|
11
|
-
describe 'included' do
|
12
|
-
let(:klass) do
|
13
|
-
Class.new { include Dry::Container::Mixin }
|
14
|
-
end
|
15
|
-
let(:container) { klass.new }
|
16
|
-
|
17
|
-
it_behaves_like 'a container'
|
18
|
-
end
|
19
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
if RUBY_ENGINE == 'rbx'
|
4
|
-
require 'codeclimate-test-reporter'
|
5
|
-
CodeClimate::TestReporter.start
|
6
|
-
end
|
7
|
-
|
8
|
-
require 'dry/container'
|
9
|
-
require 'dry/container/stub'
|
10
|
-
|
11
|
-
Dir[Pathname(__FILE__).dirname.join('support/**/*.rb').to_s].each do |file|
|
12
|
-
require file
|
13
|
-
end
|
@@ -1,479 +0,0 @@
|
|
1
|
-
shared_examples 'a container' do
|
2
|
-
describe 'configuration' do
|
3
|
-
describe 'registry' do
|
4
|
-
describe 'default' do
|
5
|
-
it { expect(klass.config.registry).to be_a(Dry::Container::Registry) }
|
6
|
-
end
|
7
|
-
|
8
|
-
describe 'custom' do
|
9
|
-
let(:custom_registry) { double('Registry') }
|
10
|
-
let(:key) { :key }
|
11
|
-
let(:item) { :item }
|
12
|
-
let(:options) { {} }
|
13
|
-
|
14
|
-
before do
|
15
|
-
klass.configure do |config|
|
16
|
-
config.registry = custom_registry
|
17
|
-
end
|
18
|
-
|
19
|
-
allow(custom_registry).to receive(:call)
|
20
|
-
end
|
21
|
-
|
22
|
-
after do
|
23
|
-
# HACK: Have to reset the configuration so that it doesn't
|
24
|
-
# interfere with other specs
|
25
|
-
klass.configure do |config|
|
26
|
-
config.registry = Dry::Container::Registry.new
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
subject! { container.register(key, item, options) }
|
31
|
-
|
32
|
-
it do
|
33
|
-
expect(custom_registry).to have_received(:call).with(
|
34
|
-
container._container,
|
35
|
-
key,
|
36
|
-
item,
|
37
|
-
options
|
38
|
-
)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe 'resolver' do
|
44
|
-
describe 'default' do
|
45
|
-
it { expect(klass.config.resolver).to be_a(Dry::Container::Resolver) }
|
46
|
-
end
|
47
|
-
|
48
|
-
describe 'custom' do
|
49
|
-
let(:custom_resolver) { double('Resolver') }
|
50
|
-
let(:item) { double('Item') }
|
51
|
-
let(:key) { :key }
|
52
|
-
|
53
|
-
before do
|
54
|
-
klass.configure do |config|
|
55
|
-
config.resolver = custom_resolver
|
56
|
-
end
|
57
|
-
|
58
|
-
allow(custom_resolver).to receive(:call).and_return(item)
|
59
|
-
end
|
60
|
-
|
61
|
-
after do
|
62
|
-
# HACK: Have to reset the configuration so that it doesn't
|
63
|
-
# interfere with other specs
|
64
|
-
klass.configure do |config|
|
65
|
-
config.resolver = Dry::Container::Resolver.new
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
subject! { container.resolve(key) }
|
70
|
-
|
71
|
-
it { expect(custom_resolver).to have_received(:call).with(container._container, key) }
|
72
|
-
it { is_expected.to eq(item) }
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
describe 'namespace_separator' do
|
77
|
-
describe 'default' do
|
78
|
-
it { expect(klass.config.namespace_separator).to eq('.') }
|
79
|
-
end
|
80
|
-
|
81
|
-
describe 'custom' do
|
82
|
-
let(:custom_registry) { double('Registry') }
|
83
|
-
let(:key) { 'key' }
|
84
|
-
let(:namespace_separator) { '-' }
|
85
|
-
let(:namespace) { 'one' }
|
86
|
-
|
87
|
-
before do
|
88
|
-
klass.configure do |config|
|
89
|
-
config.namespace_separator = namespace_separator
|
90
|
-
end
|
91
|
-
|
92
|
-
container.namespace(namespace) do
|
93
|
-
register('key', 'item')
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
after do
|
98
|
-
# HACK: Have to reset the configuration so that it doesn't
|
99
|
-
# interfere with other specs
|
100
|
-
klass.configure do |config|
|
101
|
-
config.namespace_separator = '.'
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
subject! { container.resolve([namespace, key].join(namespace_separator)) }
|
106
|
-
|
107
|
-
it { is_expected.to eq('item') }
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
context 'with default configuration' do
|
113
|
-
describe 'registering a block' do
|
114
|
-
context 'without options' do
|
115
|
-
context 'without arguments' do
|
116
|
-
it 'registers and resolves an object' do
|
117
|
-
container.register(:item) { 'item' }
|
118
|
-
|
119
|
-
expect(container.keys).to eq(['item'])
|
120
|
-
expect(container.key?(:item)).to be true
|
121
|
-
expect(container.resolve(:item)).to eq('item')
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
context 'with arguments' do
|
126
|
-
it 'registers and resolves a proc' do
|
127
|
-
container.register(:item) { |item| item }
|
128
|
-
|
129
|
-
expect(container.resolve(:item).call('item')).to eq('item')
|
130
|
-
end
|
131
|
-
|
132
|
-
it 'does not call a proc on resolving if one accepts an arbitrary number of keyword arguments' do
|
133
|
-
container.register(:item) { |**kw| 'item' }
|
134
|
-
|
135
|
-
expect(container.resolve(:item)).to be_a_kind_of Proc
|
136
|
-
expect(container.resolve(:item).call).to eq('item')
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
context 'with option call: false' do
|
142
|
-
it 'registers and resolves a proc' do
|
143
|
-
container.register(:item, call: false) { 'item' }
|
144
|
-
|
145
|
-
expect(container.keys).to eq(['item'])
|
146
|
-
expect(container.key?(:item)).to be true
|
147
|
-
expect(container.resolve(:item).call).to eq('item')
|
148
|
-
expect(container[:item].call).to eq('item')
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
describe 'registering a proc' do
|
154
|
-
context 'without options' do
|
155
|
-
context 'without arguments' do
|
156
|
-
it 'registers and resolves an object' do
|
157
|
-
container.register(:item, proc { 'item' })
|
158
|
-
|
159
|
-
expect(container.keys).to eq(['item'])
|
160
|
-
expect(container.key?(:item)).to be true
|
161
|
-
expect(container.resolve(:item)).to eq('item')
|
162
|
-
expect(container[:item]).to eq('item')
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
context 'with arguments' do
|
167
|
-
it 'registers and resolves a proc' do
|
168
|
-
container.register(:item, proc { |item| item })
|
169
|
-
|
170
|
-
expect(container.keys).to eq(['item'])
|
171
|
-
expect(container.key?(:item)).to be true
|
172
|
-
expect(container.resolve(:item).call('item')).to eq('item')
|
173
|
-
expect(container[:item].call('item')).to eq('item')
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
context 'with option call: false' do
|
179
|
-
it 'registers and resolves a proc' do
|
180
|
-
container.register(:item, proc { 'item' }, call: false)
|
181
|
-
|
182
|
-
expect(container.keys).to eq(['item'])
|
183
|
-
expect(container.key?(:item)).to be true
|
184
|
-
expect(container.resolve(:item).call).to eq('item')
|
185
|
-
expect(container[:item].call).to eq('item')
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
context 'with option memoize: true' do
|
190
|
-
it 'registers and resolves a proc' do
|
191
|
-
container.register(:item, proc { 'item' }, memoize: true)
|
192
|
-
|
193
|
-
expect(container[:item]).to be container[:item]
|
194
|
-
expect(container.keys).to eq(['item'])
|
195
|
-
expect(container.key?(:item)).to be true
|
196
|
-
expect(container.resolve(:item)).to eq('item')
|
197
|
-
expect(container[:item]).to eq('item')
|
198
|
-
end
|
199
|
-
|
200
|
-
it 'only resolves the proc once' do
|
201
|
-
resolved_times = 0
|
202
|
-
|
203
|
-
container.register(:item, proc { resolved_times += 1 }, memoize: true)
|
204
|
-
|
205
|
-
expect(container.resolve(:item)).to be 1
|
206
|
-
expect(container.resolve(:item)).to be 1
|
207
|
-
end
|
208
|
-
|
209
|
-
context 'when receiving something other than a proc' do
|
210
|
-
it do
|
211
|
-
expect { container.register(:item, 'Hello!', memoize: true) }.to raise_error(Dry::Container::Error)
|
212
|
-
end
|
213
|
-
end
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
describe 'registering an object' do
|
218
|
-
context 'without options' do
|
219
|
-
it 'registers and resolves the object' do
|
220
|
-
item = 'item'
|
221
|
-
container.register(:item, item)
|
222
|
-
|
223
|
-
expect(container.keys).to eq(['item'])
|
224
|
-
expect(container.key?(:item)).to be true
|
225
|
-
expect(container.resolve(:item)).to be(item)
|
226
|
-
expect(container[:item]).to be(item)
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
context 'with option call: false' do
|
231
|
-
it 'registers and resolves an object' do
|
232
|
-
item = -> { 'test' }
|
233
|
-
container.register(:item, item, call: false)
|
234
|
-
|
235
|
-
expect(container.keys).to eq(['item'])
|
236
|
-
expect(container.key?(:item)).to be true
|
237
|
-
expect(container.resolve(:item)).to eq(item)
|
238
|
-
expect(container[:item]).to eq(item)
|
239
|
-
end
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
describe 'registering with the same key multiple times' do
|
244
|
-
it do
|
245
|
-
container.register(:item, proc { 'item' })
|
246
|
-
|
247
|
-
expect { container.register(:item, proc { 'item' }) }.to raise_error(Dry::Container::Error)
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
describe 'resolving with a key that has not been registered' do
|
252
|
-
it do
|
253
|
-
expect(container.key?(:item)).to be false
|
254
|
-
expect { container.resolve(:item) }.to raise_error(Dry::Container::Error)
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
describe 'mixing Strings and Symbols' do
|
259
|
-
it do
|
260
|
-
container.register(:item, 'item')
|
261
|
-
expect(container.resolve('item')).to eql('item')
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
describe '#merge' do
|
266
|
-
let(:key) { :key }
|
267
|
-
let(:other) { Dry::Container.new }
|
268
|
-
|
269
|
-
before do
|
270
|
-
other.register(key) { :item }
|
271
|
-
end
|
272
|
-
|
273
|
-
context 'without namespace argument' do
|
274
|
-
subject! { container.merge(other) }
|
275
|
-
|
276
|
-
it { expect(container.resolve(key)).to be(:item) }
|
277
|
-
it { expect(container[key]).to be(:item) }
|
278
|
-
end
|
279
|
-
|
280
|
-
context 'with namespace argument' do
|
281
|
-
subject! { container.merge(other, namespace: namespace) }
|
282
|
-
|
283
|
-
context 'when namespace is nil' do
|
284
|
-
let(:namespace) { nil }
|
285
|
-
|
286
|
-
it { expect(container.resolve(key)).to be(:item) }
|
287
|
-
it { expect(container[key]).to be(:item) }
|
288
|
-
end
|
289
|
-
|
290
|
-
context 'when namespace is not nil' do
|
291
|
-
let(:namespace) { 'namespace' }
|
292
|
-
|
293
|
-
it { expect(container.resolve("#{namespace}.#{key}")).to be(:item) }
|
294
|
-
it { expect(container["#{namespace}.#{key}"]).to be(:item) }
|
295
|
-
end
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
describe '#key?' do
|
300
|
-
let(:key) { :key }
|
301
|
-
|
302
|
-
before do
|
303
|
-
container.register(key) { :item }
|
304
|
-
end
|
305
|
-
|
306
|
-
subject! { container.key?(resolve_key) }
|
307
|
-
|
308
|
-
context 'when key exists in container' do
|
309
|
-
let(:resolve_key) { key }
|
310
|
-
|
311
|
-
it { is_expected.to be true }
|
312
|
-
end
|
313
|
-
|
314
|
-
context 'when key does not exist in container' do
|
315
|
-
let(:resolve_key) { :random }
|
316
|
-
|
317
|
-
it { is_expected.to be false }
|
318
|
-
end
|
319
|
-
end
|
320
|
-
|
321
|
-
describe '#keys' do
|
322
|
-
let(:keys) { [:key_1, :key_2] }
|
323
|
-
let(:expected_keys) { ['key_1', 'key_2'] }
|
324
|
-
|
325
|
-
before do
|
326
|
-
keys.each do |key|
|
327
|
-
container.register(key) { :item }
|
328
|
-
end
|
329
|
-
end
|
330
|
-
|
331
|
-
subject! { container.keys }
|
332
|
-
|
333
|
-
it 'returns stringified versions of all registered keys' do
|
334
|
-
is_expected.to match_array(expected_keys)
|
335
|
-
end
|
336
|
-
end
|
337
|
-
|
338
|
-
describe '#each_key' do
|
339
|
-
let(:keys) { [:key_1, :key_2] }
|
340
|
-
let(:expected_keys) { ['key_1', 'key_2'] }
|
341
|
-
let!(:yielded_keys) { [] }
|
342
|
-
|
343
|
-
before do
|
344
|
-
keys.each do |key|
|
345
|
-
container.register(key) { :item }
|
346
|
-
end
|
347
|
-
end
|
348
|
-
|
349
|
-
subject! do
|
350
|
-
container.each_key { |key| yielded_keys << key }
|
351
|
-
end
|
352
|
-
|
353
|
-
it 'yields stringified versions of all registered keys to the block' do
|
354
|
-
expect(yielded_keys).to match_array(expected_keys)
|
355
|
-
end
|
356
|
-
|
357
|
-
it 'returns the container' do
|
358
|
-
is_expected.to eq(container)
|
359
|
-
end
|
360
|
-
end
|
361
|
-
|
362
|
-
describe 'namespace' do
|
363
|
-
context 'when block does not take arguments' do
|
364
|
-
before do
|
365
|
-
container.namespace('one') do
|
366
|
-
register('two', 2)
|
367
|
-
end
|
368
|
-
end
|
369
|
-
|
370
|
-
subject! { container.resolve('one.two') }
|
371
|
-
|
372
|
-
it 'registers items under the given namespace' do
|
373
|
-
is_expected.to eq(2)
|
374
|
-
end
|
375
|
-
end
|
376
|
-
|
377
|
-
context 'when block takes arguments' do
|
378
|
-
before do
|
379
|
-
container.namespace('one') do |c|
|
380
|
-
c.register('two', 2)
|
381
|
-
end
|
382
|
-
end
|
383
|
-
|
384
|
-
subject! { container.resolve('one.two') }
|
385
|
-
|
386
|
-
it 'registers items under the given namespace' do
|
387
|
-
is_expected.to eq(2)
|
388
|
-
end
|
389
|
-
end
|
390
|
-
|
391
|
-
context 'with nesting' do
|
392
|
-
before do
|
393
|
-
container.namespace('one') do
|
394
|
-
namespace('two') do
|
395
|
-
register('three', 3)
|
396
|
-
end
|
397
|
-
end
|
398
|
-
end
|
399
|
-
|
400
|
-
subject! { container.resolve('one.two.three') }
|
401
|
-
|
402
|
-
it 'registers items under the given namespaces' do
|
403
|
-
is_expected.to eq(3)
|
404
|
-
end
|
405
|
-
end
|
406
|
-
end
|
407
|
-
|
408
|
-
describe 'import' do
|
409
|
-
it 'allows importing of namespaces' do
|
410
|
-
ns = Dry::Container::Namespace.new('one') do
|
411
|
-
register('two', 2)
|
412
|
-
end
|
413
|
-
|
414
|
-
container.import(ns)
|
415
|
-
|
416
|
-
expect(container.resolve('one.two')).to eq(2)
|
417
|
-
end
|
418
|
-
|
419
|
-
it 'allows importing of nested namespaces' do
|
420
|
-
ns = Dry::Container::Namespace.new('two') do
|
421
|
-
register('three', 3)
|
422
|
-
end
|
423
|
-
|
424
|
-
container.namespace('one') do
|
425
|
-
import(ns)
|
426
|
-
end
|
427
|
-
|
428
|
-
expect(container.resolve('one.two.three')).to eq(3)
|
429
|
-
end
|
430
|
-
end
|
431
|
-
end
|
432
|
-
|
433
|
-
describe 'stubbing' do
|
434
|
-
before do
|
435
|
-
container.enable_stubs!
|
436
|
-
|
437
|
-
container.register(:item, 'item')
|
438
|
-
container.register(:foo, 'bar')
|
439
|
-
end
|
440
|
-
|
441
|
-
after do
|
442
|
-
container.unstub
|
443
|
-
end
|
444
|
-
|
445
|
-
it 'keys can be stubbed' do
|
446
|
-
container.stub(:item, 'stub')
|
447
|
-
expect(container.resolve(:item)).to eql('stub')
|
448
|
-
expect(container[:item]).to eql('stub')
|
449
|
-
end
|
450
|
-
|
451
|
-
it 'only other keys remain accesible' do
|
452
|
-
container.stub(:item, 'stub')
|
453
|
-
expect(container.resolve(:foo)).to eql('bar')
|
454
|
-
expect(container[:foo]).to eql('bar')
|
455
|
-
end
|
456
|
-
|
457
|
-
it 'keys can be reverted back to their original value' do
|
458
|
-
container.stub(:item, 'stub')
|
459
|
-
container.unstub(:item)
|
460
|
-
|
461
|
-
expect(container.resolve(:item)).to eql('item')
|
462
|
-
expect(container[:item]).to eql('item')
|
463
|
-
end
|
464
|
-
|
465
|
-
describe 'with block argument' do
|
466
|
-
it 'executes the block with the given stubs' do
|
467
|
-
expect { |b| container.stub(:item, 'stub', &b) }.to yield_control
|
468
|
-
end
|
469
|
-
|
470
|
-
it 'keys are stubbed only while inside the block' do
|
471
|
-
container.stub(:item, 'stub') do
|
472
|
-
expect(container.resolve(:item)).to eql('stub')
|
473
|
-
end
|
474
|
-
|
475
|
-
expect(container.resolve(:item)).to eql('item')
|
476
|
-
end
|
477
|
-
end
|
478
|
-
end
|
479
|
-
end
|