dry-container 0.2.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25d869f2ebe16d1f8fdb1d1516c501def7aaeb94
4
- data.tar.gz: 596fc6859c7fef0f70c40aae379eea4a632e59fa
3
+ metadata.gz: 5e3d3bf439e6c3bd3b03269a0b1b078df324550a
4
+ data.tar.gz: 43daf4430f1496b7444becd4619db45a1f5c47ea
5
5
  SHA512:
6
- metadata.gz: 7b80fbff0503dcb46b0c026c93643abab69fa0f11517b85280f46878217bd8edeef96d4b2f14d6c29b4b25e2f7e87186e213cce01a2b26b4d8b820ffdd3b3196
7
- data.tar.gz: f237d2987d1b195abf05afc0537abb46980427dccc465371fceace2cacab63589411f3f957474e81dd0fcf8371807f570b433c6aadebdcee71160f5f33d18e30
6
+ metadata.gz: 3eb73cfbddb9a59a929c1e6a27dc5087830c48733077f584f77cc974c72a0f5661ae2a42d78899ecec9c3e9bba3ddb134bffae771442258a8fa8eb31b08e61a8
7
+ data.tar.gz: ec5253b3eb128f50420befe89f40a9855b22a60638fb323a9fba34f16a7744dfa5eaab1a5522e6442b682de3863022445839376da96c4f193c0cc7ce5817f423
data/README.md CHANGED
@@ -10,6 +10,20 @@ A simple, configurable container implemented in Ruby
10
10
 
11
11
  ## Synopsis
12
12
 
13
+ ### Brief Example
14
+
15
+ ```ruby
16
+ container = Dry::Container.new
17
+ container.register(:parrot) { |a| puts a }
18
+
19
+ parrot = container.resolve(:parrot)
20
+ parrot.call("Hello World")
21
+ # Hello World
22
+ # => nil
23
+ ```
24
+
25
+ ### Detailed Example
26
+
13
27
  ```ruby
14
28
  User = Struct.new(:name, :email)
15
29
 
@@ -105,7 +105,7 @@ module Dry
105
105
  def resolve(key)
106
106
  config.resolver.call(_container, key)
107
107
  end
108
- alias_method :[], :resolve
108
+ alias [] resolve
109
109
 
110
110
  # Merge in the items of the other container
111
111
  #
@@ -132,6 +132,15 @@ module Dry
132
132
  config.resolver.key?(_container, key)
133
133
  end
134
134
 
135
+ # An array of registered names for the container
136
+ #
137
+ # @return [Array<String>]
138
+ #
139
+ # @api public
140
+ def keys
141
+ config.resolver.keys(_container)
142
+ end
143
+
135
144
  # Evaluate block and register items in namespace
136
145
  #
137
146
  # @param [Mixed] namespace
@@ -25,7 +25,7 @@ module Dry
25
25
  if block.arity.zero?
26
26
  instance_eval(&block)
27
27
  else
28
- block.call(self)
28
+ yield self
29
29
  end
30
30
  end
31
31
 
@@ -28,13 +28,13 @@ module Dry
28
28
  #
29
29
  # @api public
30
30
  def call(container, key, item, options)
31
+ key = key.to_s.dup.freeze
31
32
  @_mutex.synchronize do
32
33
  if container.key?(key)
33
- fail Error, "There is already an item registered with the key #{key.inspect}"
34
- else
35
- key = key.is_a?(::String) ? key.dup.freeze : key
36
- container[key] = ::Dry::Container::Item.new(item, options)
34
+ raise Error, "There is already an item registered with the key #{key.inspect}"
37
35
  end
36
+
37
+ container[key] = ::Dry::Container::Item.new(item, options)
38
38
  end
39
39
  end
40
40
  end
@@ -18,8 +18,8 @@ module Dry
18
18
  #
19
19
  # @api public
20
20
  def call(container, key)
21
- item = container.fetch(key) do
22
- fail Error, "Nothing registered with the key #{key.inspect}"
21
+ item = container.fetch(key.to_s) do
22
+ raise Error, "Nothing registered with the key #{key.inspect}"
23
23
  end
24
24
 
25
25
  item.call
@@ -36,7 +36,16 @@ module Dry
36
36
  #
37
37
  # @api public
38
38
  def key?(container, key)
39
- container.key?(key)
39
+ container.key?(key.to_s)
40
+ end
41
+
42
+ # An array of registered names for the container
43
+ #
44
+ # @return [Array]
45
+ #
46
+ # @api public
47
+ def keys(container)
48
+ container.keys
40
49
  end
41
50
  end
42
51
  end
@@ -0,0 +1,48 @@
1
+ module Dry
2
+ class Container
3
+ module Stub
4
+ # Overrides resolve to look into stubbed keys first
5
+ #
6
+ # @api public
7
+ def resolve(key)
8
+ _stubs.fetch(key) { super }
9
+ end
10
+
11
+ # Add a stub to the container
12
+ def stub(key, value, &block)
13
+ _stubs[key] = value
14
+
15
+ if block
16
+ yield
17
+ unstub(key)
18
+ end
19
+
20
+ self
21
+ end
22
+
23
+ # Remove stubbed keys from the container
24
+ def unstub(*keys)
25
+ keys = _stubs.keys if keys.empty?
26
+ keys.each { |key| _stubs.delete(key) }
27
+ end
28
+
29
+ # Stubs have already been enabled turning this into a noop
30
+ def enable_stubs!
31
+ end
32
+
33
+ private
34
+
35
+ # Stubs container
36
+ def _stubs
37
+ @_stubs ||= {}
38
+ end
39
+ end
40
+
41
+ module Mixin
42
+ # Enable stubbing functionality into the current container
43
+ def enable_stubs!
44
+ extend Dry::Container::Stub
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,6 +1,6 @@
1
1
  module Dry
2
2
  class Container
3
3
  # @api public
4
- VERSION = '0.2.8'.freeze
4
+ VERSION = '0.3.0'.freeze
5
5
  end
6
6
  end
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'dry/container'
4
+ require 'dry/container/stub'
4
5
 
5
6
  Dir[Pathname(__FILE__).dirname.join('support/**/*.rb').to_s].each do |file|
6
7
  require file
@@ -116,6 +116,7 @@ shared_examples 'a container' do
116
116
  it 'registers and resolves an object' do
117
117
  container.register(:item) { 'item' }
118
118
 
119
+ expect(container.keys).to eq(['item'])
119
120
  expect(container.key?(:item)).to be true
120
121
  expect(container.resolve(:item)).to eq('item')
121
122
  end
@@ -134,6 +135,7 @@ shared_examples 'a container' do
134
135
  it 'registers and resolves a proc' do
135
136
  container.register(:item, call: false) { 'item' }
136
137
 
138
+ expect(container.keys).to eq(['item'])
137
139
  expect(container.key?(:item)).to be true
138
140
  expect(container.resolve(:item).call).to eq('item')
139
141
  expect(container[:item].call).to eq('item')
@@ -147,6 +149,7 @@ shared_examples 'a container' do
147
149
  it 'registers and resolves an object' do
148
150
  container.register(:item, proc { 'item' })
149
151
 
152
+ expect(container.keys).to eq(['item'])
150
153
  expect(container.key?(:item)).to be true
151
154
  expect(container.resolve(:item)).to eq('item')
152
155
  expect(container[:item]).to eq('item')
@@ -157,6 +160,7 @@ shared_examples 'a container' do
157
160
  it 'registers and resolves a proc' do
158
161
  container.register(:item, proc { |item| item })
159
162
 
163
+ expect(container.keys).to eq(['item'])
160
164
  expect(container.key?(:item)).to be true
161
165
  expect(container.resolve(:item).call('item')).to eq('item')
162
166
  expect(container[:item].call('item')).to eq('item')
@@ -168,6 +172,7 @@ shared_examples 'a container' do
168
172
  it 'registers and resolves a proc' do
169
173
  container.register(:item, proc { 'item' }, call: false)
170
174
 
175
+ expect(container.keys).to eq(['item'])
171
176
  expect(container.key?(:item)).to be true
172
177
  expect(container.resolve(:item).call).to eq('item')
173
178
  expect(container[:item].call).to eq('item')
@@ -181,6 +186,7 @@ shared_examples 'a container' do
181
186
  item = 'item'
182
187
  container.register(:item, item)
183
188
 
189
+ expect(container.keys).to eq(['item'])
184
190
  expect(container.key?(:item)).to be true
185
191
  expect(container.resolve(:item)).to be(item)
186
192
  expect(container[:item]).to be(item)
@@ -192,6 +198,7 @@ shared_examples 'a container' do
192
198
  item = -> { 'test' }
193
199
  container.register(:item, item, call: false)
194
200
 
201
+ expect(container.keys).to eq(['item'])
195
202
  expect(container.key?(:item)).to be true
196
203
  expect(container.resolve(:item)).to eq(item)
197
204
  expect(container[:item]).to eq(item)
@@ -214,6 +221,13 @@ shared_examples 'a container' do
214
221
  end
215
222
  end
216
223
 
224
+ describe 'mixing Strings and Symbols' do
225
+ it do
226
+ container.register(:item, 'item')
227
+ expect(container.resolve('item')).to eql('item')
228
+ end
229
+ end
230
+
217
231
  describe '#merge' do
218
232
  let(:key) { :key }
219
233
  let(:other) { Dry::Container.new }
@@ -299,4 +313,48 @@ shared_examples 'a container' do
299
313
  end
300
314
  end
301
315
  end
316
+
317
+ describe 'stubbing' do
318
+ before do
319
+ container.enable_stubs!
320
+
321
+ container.register(:item, 'item')
322
+ container.register(:foo, 'bar')
323
+ end
324
+
325
+ after do
326
+ container.unstub
327
+ end
328
+
329
+ it 'keys can be stubbed' do
330
+ container.stub(:item, 'stub')
331
+ expect(container.resolve(:item)).to eql('stub')
332
+ end
333
+
334
+ it 'only other keys remain accesible' do
335
+ container.stub(:item, 'stub')
336
+ expect(container.resolve(:foo)).to eql('bar')
337
+ end
338
+
339
+ it 'keys can be reverted back to their original value' do
340
+ container.stub(:item, 'stub')
341
+ container.unstub(:item)
342
+
343
+ expect(container.resolve(:item)).to eql('item')
344
+ end
345
+
346
+ describe 'with block argument' do
347
+ it 'executes the block with the given stubs' do
348
+ expect { |b| container.stub(:item, 'stub', &b) }.to yield_control
349
+ end
350
+
351
+ it 'keys are stubbed only while inside the block' do
352
+ container.stub(:item, 'stub') do
353
+ expect(container.resolve(:item)).to eql('stub')
354
+ end
355
+
356
+ expect(container.resolve(:item)).to eql('item')
357
+ end
358
+ end
359
+ end
302
360
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-container
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Holland
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-20 00:00:00.000000000 Z
11
+ date: 2016-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thread_safe
@@ -112,6 +112,7 @@ files:
112
112
  - lib/dry/container/namespace_dsl.rb
113
113
  - lib/dry/container/registry.rb
114
114
  - lib/dry/container/resolver.rb
115
+ - lib/dry/container/stub.rb
115
116
  - lib/dry/container/version.rb
116
117
  - rakelib/rubocop.rake
117
118
  - spec/integration/container_spec.rb