dry-container 0.3.4 → 0.4.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: 2fb7b27d123e0eeca646cadab96b2f4ffb23de85
4
- data.tar.gz: da29556aeb02ed91cf5ee90b9cfccbbf21b86514
3
+ metadata.gz: 674ad0ad74a6d746feaad68d60327231693da1af
4
+ data.tar.gz: 7f61c7b298cbccb1f0c0b34d7df4a116d0a500ef
5
5
  SHA512:
6
- metadata.gz: 74e4defaad4bdcbafe10b718f8efd8c76c9e480dc6681546889a762184b732da205e98f3c3cbe4083e474642ecc43cb6308ccf1f57b739ea25218e767e5b892d
7
- data.tar.gz: 1335f6b7e9ea69d245b8a51cfd3545a1e915436933a9ef83bfcb57f0bc3a79e5f9f53a6dd87f6244a86b6682fe2656a630c63e00f9e688a6fdc73b194e0bba9c
6
+ metadata.gz: d794c0b798bb18519f10b7625f351193a3e768c7870d57a95adc76cb1d2e4a2c45a679622349c2c3404922346a3108b6c42cafe9a51b7a51dc34790c98ad9f89
7
+ data.tar.gz: 5581b7fff68c5eb4e50a8bdd949043b54464f6f103eb6719bc9a6b7ad27a57e077d4b8118d76df8d929bd6d67af2f39fe396505f106263948a74bde96b1bf826
@@ -3,7 +3,7 @@ require File.expand_path('../lib/dry/container/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = 'dry-container'
6
- spec.version = Dry::Container::VERSION
6
+ spec.version = ::Dry::Container::VERSION
7
7
  spec.authors = ['Andy Holland']
8
8
  spec.email = ['andyholland1991@aol.com']
9
9
  spec.summary = 'A simple container intended for use as an IoC container'
data/lib/dry/container.rb CHANGED
@@ -31,6 +31,6 @@ module Dry
31
31
  #
32
32
  # @api public
33
33
  class Container
34
- include Mixin
34
+ include ::Dry::Container::Mixin
35
35
  end
36
36
  end
@@ -1,6 +1,6 @@
1
1
  module Dry
2
2
  class Container
3
3
  # @api public
4
- class Error < StandardError; end
4
+ class Error < ::StandardError; end
5
5
  end
6
6
  end
@@ -1,5 +1,8 @@
1
1
  module Dry
2
2
  class Container
3
+ PREFIX_NAMESPACE = ->(namespace, key, config) do
4
+ [namespace, key].compact.join(config.namespace_separator)
5
+ end
3
6
  # Mixin to expose Inversion of Control (IoC) container behaviour
4
7
  #
5
8
  # @example
@@ -26,7 +29,7 @@ module Dry
26
29
  module Mixin
27
30
  # @private
28
31
  def self.extended(base)
29
- hooks_mod = Module.new do
32
+ hooks_mod = ::Module.new do
30
33
  def inherited(subclass)
31
34
  subclass.instance_variable_set(:@_container, @_container.dup)
32
35
  super
@@ -126,8 +129,12 @@ module Dry
126
129
  # @return [Dry::Container::Mixin] self
127
130
  #
128
131
  # @api public
129
- def merge(other)
130
- _container.merge!(other._container)
132
+ def merge(other, namespace: nil)
133
+ _container.merge!(
134
+ other._container.each_with_object(::Concurrent::Hash.new) do |a, h|
135
+ h[PREFIX_NAMESPACE.call(namespace, a.first, config)] = a.last
136
+ end
137
+ )
131
138
  self
132
139
  end
133
140
 
@@ -152,6 +159,18 @@ module Dry
152
159
  config.resolver.keys(_container)
153
160
  end
154
161
 
162
+ # Calls block once for each key in container, passing the key as a parameter.
163
+ #
164
+ # If no block is given, an enumerator is returned instead.
165
+ #
166
+ # @return [Dry::Container::Mixin] self
167
+ #
168
+ # @api public
169
+ def each_key(&block)
170
+ config.resolver.each_key(_container, &block)
171
+ self
172
+ end
173
+
155
174
  # Evaluate block and register items in namespace
156
175
  #
157
176
  # @param [Mixed] namespace
@@ -47,6 +47,18 @@ module Dry
47
47
  def keys(container)
48
48
  container.keys
49
49
  end
50
+
51
+
52
+ # Calls block once for each key in container, passing the key as a parameter.
53
+ #
54
+ # If no block is given, an enumerator is returned instead.
55
+ #
56
+ # @return Hash
57
+ #
58
+ # @api public
59
+ def each_key(container, &block)
60
+ container.each_key(&block)
61
+ end
50
62
  end
51
63
  end
52
64
  end
@@ -41,7 +41,7 @@ module Dry
41
41
  module Mixin
42
42
  # Enable stubbing functionality into the current container
43
43
  def enable_stubs!
44
- extend Dry::Container::Stub
44
+ extend ::Dry::Container::Stub
45
45
  end
46
46
  end
47
47
  end
@@ -1,6 +1,6 @@
1
1
  module Dry
2
2
  class Container
3
3
  # @api public
4
- VERSION = '0.3.4'.freeze
4
+ VERSION = '0.4.0'.freeze
5
5
  end
6
6
  end
@@ -243,11 +243,93 @@ shared_examples 'a container' do
243
243
  other.register(key) { :item }
244
244
  end
245
245
 
246
- subject! { container.merge(other) }
246
+ context 'without namespace argument' do
247
+ subject! { container.merge(other) }
247
248
 
248
- it { expect(container.key?(key)).to be true }
249
- it { expect(container.resolve(key)).to be(:item) }
250
- it { expect(container[key]).to be(:item) }
249
+ it { expect(container.resolve(key)).to be(:item) }
250
+ it { expect(container[key]).to be(:item) }
251
+ end
252
+
253
+ context 'with namespace argument' do
254
+ subject! { container.merge(other, namespace: namespace) }
255
+
256
+ context 'when namespace is nil' do
257
+ let(:namespace) { nil }
258
+
259
+ it { expect(container.resolve(key)).to be(:item) }
260
+ it { expect(container[key]).to be(:item) }
261
+ end
262
+
263
+ context 'when namespace is not nil' do
264
+ let(:namespace) { 'namespace' }
265
+
266
+ it { expect(container.resolve("#{namespace}.#{key}")).to be(:item) }
267
+ it { expect(container["#{namespace}.#{key}"]).to be(:item) }
268
+ end
269
+ end
270
+ end
271
+
272
+ describe '#key?' do
273
+ let(:key) { :key }
274
+
275
+ before do
276
+ container.register(key) { :item }
277
+ end
278
+
279
+ subject! { container.key?(resolve_key) }
280
+
281
+ context 'when key exists in container' do
282
+ let(:resolve_key) { key }
283
+
284
+ it { is_expected.to be true }
285
+ end
286
+
287
+ context 'when key does not exist in container' do
288
+ let(:resolve_key) { :random }
289
+
290
+ it { is_expected.to be false }
291
+ end
292
+ end
293
+
294
+ describe '#keys' do
295
+ let(:keys) { [:key_1, :key_2] }
296
+ let(:expected_keys) { ['key_1', 'key_2'] }
297
+
298
+ before do
299
+ keys.each do |key|
300
+ container.register(key) { :item }
301
+ end
302
+ end
303
+
304
+ subject! { container.keys }
305
+
306
+ it 'returns stringified versions of all registered keys' do
307
+ is_expected.to match_array(expected_keys)
308
+ end
309
+ end
310
+
311
+ describe '#each_key' do
312
+ let(:keys) { [:key_1, :key_2] }
313
+ let(:expected_keys) { ['key_1', 'key_2'] }
314
+ let!(:yielded_keys) { [] }
315
+
316
+ before do
317
+ keys.each do |key|
318
+ container.register(key) { :item }
319
+ end
320
+ end
321
+
322
+ subject! do
323
+ container.each_key { |key| yielded_keys << key }
324
+ end
325
+
326
+ it 'yields stringified versions of all registered keys to the block' do
327
+ expect(yielded_keys).to match_array(expected_keys)
328
+ end
329
+
330
+ it 'returns the container' do
331
+ is_expected.to eq(container)
332
+ end
251
333
  end
252
334
 
253
335
  describe 'namespace' do
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.3.4
4
+ version: 0.4.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-05-24 00:00:00.000000000 Z
11
+ date: 2016-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -149,3 +149,4 @@ test_files:
149
149
  - spec/integration/mixin_spec.rb
150
150
  - spec/spec_helper.rb
151
151
  - spec/support/shared_examples/container.rb
152
+ has_rdoc: