dry-system 0.7.2 → 0.7.3
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 +4 -4
- data/CHANGELOG.md +7 -0
- data/lib/dry/system/container.rb +15 -3
- data/lib/dry/system/stubs.rb +17 -4
- data/lib/dry/system/version.rb +1 -1
- data/spec/fixtures/stubbing/lib/{stubbing → test}/car.rb +1 -1
- data/spec/fixtures/stubbing/system/boot/db.rb +8 -0
- data/spec/integration/import_spec.rb +18 -0
- data/spec/unit/container_spec.rb +22 -20
- metadata +5 -5
- data/spec/fixtures/stubbing/system/boot/mock.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94d4f0b0085ef8e7edf3938fe94d39f4c26d2ea9
|
4
|
+
data.tar.gz: 15a9499b38c9d1a8ef109728d86b577616dad388
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4979edbd2d31b3039bc25438a2340462ce94e946e9ceeded7003c488e8322209a07b1d58ccfd5419a922ac1a96cf326bf8dddc5fa22069b82387e9578564a944
|
7
|
+
data.tar.gz: 887221bf029d0b6cf26f32e06ff8ac8d642e9cdc289cf8428351f8185a73161900ee26ee762d778f1912c0f7926c936e397cef28db3afb4b2d2c5ddda38518de
|
data/CHANGELOG.md
CHANGED
data/lib/dry/system/container.rb
CHANGED
@@ -80,6 +80,7 @@ module Dry
|
|
80
80
|
|
81
81
|
class << self
|
82
82
|
extend Dry::Core::Deprecations['Dry::System::Container']
|
83
|
+
|
83
84
|
# Configures the container
|
84
85
|
#
|
85
86
|
# @example
|
@@ -213,6 +214,15 @@ module Dry
|
|
213
214
|
self
|
214
215
|
end
|
215
216
|
|
217
|
+
# Return if a container was finalized
|
218
|
+
#
|
219
|
+
# @return [TrueClass, FalseClass]
|
220
|
+
#
|
221
|
+
# @api public
|
222
|
+
def finalized?
|
223
|
+
@__finalized__.equal?(true)
|
224
|
+
end
|
225
|
+
|
216
226
|
# Finalizes the container
|
217
227
|
#
|
218
228
|
# This triggers importing components from other containers, booting
|
@@ -243,7 +253,7 @@ module Dry
|
|
243
253
|
#
|
244
254
|
# @api public
|
245
255
|
def finalize!(freeze: true, &block)
|
246
|
-
return self if
|
256
|
+
return self if finalized?
|
247
257
|
|
248
258
|
yield(self) if block
|
249
259
|
|
@@ -252,6 +262,8 @@ module Dry
|
|
252
262
|
manual_registrar.finalize!
|
253
263
|
auto_registrar.finalize!
|
254
264
|
|
265
|
+
@__finalized__ = true
|
266
|
+
|
255
267
|
self.freeze if freeze
|
256
268
|
end
|
257
269
|
|
@@ -433,7 +445,7 @@ module Dry
|
|
433
445
|
|
434
446
|
# @api public
|
435
447
|
def resolve(key)
|
436
|
-
load_component(key) unless
|
448
|
+
load_component(key) unless finalized?
|
437
449
|
|
438
450
|
super
|
439
451
|
end
|
@@ -509,7 +521,7 @@ module Dry
|
|
509
521
|
# @api private
|
510
522
|
def load_local_component(component, default_namespace_fallback = false)
|
511
523
|
if component.bootable?(booter.path) || component.file_exists?(load_paths)
|
512
|
-
booter.boot_dependency(component) unless
|
524
|
+
booter.boot_dependency(component) unless finalized?
|
513
525
|
|
514
526
|
require_component(component) do
|
515
527
|
register(component.identifier) { component.instance }
|
data/lib/dry/system/stubs.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
+
require 'dry/container/stub'
|
2
|
+
|
1
3
|
module Dry
|
2
4
|
module System
|
3
5
|
class Container
|
4
|
-
# Incuded only in the Test environment
|
5
|
-
# Sending the message enable_stubs! allow you to stub components after
|
6
|
-
# finalize your container in your tests.
|
7
|
-
#
|
8
6
|
# @api private
|
9
7
|
module Stubs
|
10
8
|
def finalize!(&block)
|
@@ -12,8 +10,23 @@ module Dry
|
|
12
10
|
end
|
13
11
|
end
|
14
12
|
|
13
|
+
# Enables stubbing container's components
|
14
|
+
#
|
15
|
+
# @example
|
16
|
+
# require 'dry/system/stubs'
|
17
|
+
#
|
18
|
+
# MyContainer.enable_stubs!
|
19
|
+
# MyContainer.finalize!
|
20
|
+
#
|
21
|
+
# MyContainer.stub('some.component', some_stub_object)
|
22
|
+
#
|
23
|
+
# @return Container
|
24
|
+
#
|
25
|
+
# @api public
|
15
26
|
def self.enable_stubs!
|
27
|
+
super
|
16
28
|
extend ::Dry::System::Container::Stubs
|
29
|
+
self
|
17
30
|
end
|
18
31
|
end
|
19
32
|
end
|
data/lib/dry/system/version.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'dry/system/stubs'
|
2
|
+
|
1
3
|
RSpec.describe 'Lazy-booting external deps' do
|
2
4
|
before do
|
3
5
|
module Test
|
@@ -42,6 +44,14 @@ RSpec.describe 'Lazy-booting external deps' do
|
|
42
44
|
end
|
43
45
|
|
44
46
|
it_behaves_like 'lazy booted dependency'
|
47
|
+
|
48
|
+
context 'when stubs are enabled' do
|
49
|
+
before do
|
50
|
+
system.enable_stubs!
|
51
|
+
end
|
52
|
+
|
53
|
+
it_behaves_like 'lazy booted dependency'
|
54
|
+
end
|
45
55
|
end
|
46
56
|
|
47
57
|
context 'when top-level container provides the dependency through import' do
|
@@ -59,5 +69,13 @@ RSpec.describe 'Lazy-booting external deps' do
|
|
59
69
|
end
|
60
70
|
|
61
71
|
it_behaves_like 'lazy booted dependency'
|
72
|
+
|
73
|
+
context 'when stubs are enabled' do
|
74
|
+
before do
|
75
|
+
system.enable_stubs!
|
76
|
+
end
|
77
|
+
|
78
|
+
it_behaves_like 'lazy booted dependency'
|
79
|
+
end
|
62
80
|
end
|
63
81
|
end
|
data/spec/unit/container_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'dry/system/container'
|
2
|
+
require 'dry/system/stubs'
|
2
3
|
|
3
4
|
RSpec.describe Dry::System::Container do
|
4
5
|
subject(:container) { Test::Container }
|
@@ -196,46 +197,47 @@ RSpec.describe Dry::System::Container do
|
|
196
197
|
end
|
197
198
|
end
|
198
199
|
|
199
|
-
|
200
|
+
describe '.stub' do
|
201
|
+
let(:stubbed_car) do
|
202
|
+
instance_double(Test::Car, wheels_count: 5)
|
203
|
+
end
|
204
|
+
|
200
205
|
before do
|
201
206
|
class Test::Container < Dry::System::Container
|
202
207
|
configure do |config|
|
203
208
|
config.root = SPEC_ROOT.join('fixtures/stubbing').realpath
|
204
209
|
end
|
210
|
+
|
205
211
|
load_paths!('lib')
|
206
212
|
auto_register!('lib')
|
207
213
|
end
|
208
214
|
end
|
209
215
|
|
210
|
-
describe '
|
211
|
-
|
212
|
-
container.
|
213
|
-
end
|
214
|
-
|
215
|
-
it 'raises error when trying to stub freeze container' do
|
216
|
-
expect {
|
217
|
-
allow(container).to receive(:[]).with('mock').and_return(true)
|
218
|
-
}.to raise_error(RuntimeError, /frozen/)
|
216
|
+
describe 'with stubs disabled' do
|
217
|
+
it 'raises error when trying to stub frozen container' do
|
218
|
+
expect { container.stub('test.car', stubbed_car) }.to raise_error(NoMethodError, /stub/)
|
219
219
|
end
|
220
220
|
end
|
221
221
|
|
222
|
-
describe 'with
|
222
|
+
describe 'with stubs enabled' do
|
223
223
|
before do
|
224
224
|
container.enable_stubs!
|
225
|
-
container.finalize!
|
226
225
|
end
|
227
226
|
|
228
|
-
it '
|
229
|
-
expect(container[
|
230
|
-
|
231
|
-
expect(container[
|
227
|
+
it 'lazy-loads a component' do
|
228
|
+
expect(container[:db]).to be_instance_of(Test::DB)
|
229
|
+
container.finalize!
|
230
|
+
expect(container[:db]).to be_instance_of(Test::DB)
|
232
231
|
end
|
233
232
|
|
234
233
|
it 'allows to stub components' do
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
234
|
+
container.finalize!
|
235
|
+
|
236
|
+
expect(container['test.car'].wheels_count).to be(4)
|
237
|
+
|
238
|
+
container.stub('test.car', stubbed_car)
|
239
|
+
|
240
|
+
expect(container['test.car'].wheels_count).to be(5)
|
239
241
|
end
|
240
242
|
end
|
241
243
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-system
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
@@ -232,8 +232,8 @@ files:
|
|
232
232
|
- spec/fixtures/other/lib/test/models.rb
|
233
233
|
- spec/fixtures/other/lib/test/models/book.rb
|
234
234
|
- spec/fixtures/other/lib/test/models/user.rb
|
235
|
-
- spec/fixtures/stubbing/lib/
|
236
|
-
- spec/fixtures/stubbing/system/boot/
|
235
|
+
- spec/fixtures/stubbing/lib/test/car.rb
|
236
|
+
- spec/fixtures/stubbing/system/boot/db.rb
|
237
237
|
- spec/fixtures/test/config/application.yml
|
238
238
|
- spec/fixtures/test/config/subapp.yml
|
239
239
|
- spec/fixtures/test/lib/test/dep.rb
|
@@ -317,8 +317,8 @@ test_files:
|
|
317
317
|
- spec/fixtures/other/lib/test/models.rb
|
318
318
|
- spec/fixtures/other/lib/test/models/book.rb
|
319
319
|
- spec/fixtures/other/lib/test/models/user.rb
|
320
|
-
- spec/fixtures/stubbing/lib/
|
321
|
-
- spec/fixtures/stubbing/system/boot/
|
320
|
+
- spec/fixtures/stubbing/lib/test/car.rb
|
321
|
+
- spec/fixtures/stubbing/system/boot/db.rb
|
322
322
|
- spec/fixtures/test/config/application.yml
|
323
323
|
- spec/fixtures/test/config/subapp.yml
|
324
324
|
- spec/fixtures/test/lib/test/dep.rb
|