smart_ioc 0.1.27 → 0.1.28
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/Gemfile.lock +1 -1
- data/lib/smart_ioc/bean_definitions_storage.rb +8 -0
- data/lib/smart_ioc/bean_factory.rb +37 -30
- data/lib/smart_ioc/container.rb +1 -0
- data/lib/smart_ioc/version.rb +1 -1
- data/spec/smart_ioc/factory_method_spec.rb +42 -0
- data/spec/smart_ioc/smart_ioc_spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 241dbe4a31d95181db8b2604a570f5e0f244894e
|
4
|
+
data.tar.gz: 08434ffb62dd2727da1d82fa6919f69263593117
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 164dde71578f2399b63af9af1fc173bc261c0ac19d4c2eccfb44a300e5cacbd1ce9e7f76ae3843a79734cae1333ba0a03fb0cbb599c08302af1c7122ea0443ff
|
7
|
+
data.tar.gz: a4d77468cfed50a70dbeecd23366ac05e4f7ac93a76ba8453336a298ea9e2ef9d8885a00b059500464a1c691133f87d54d9f685cbc88ec0768c6272dfafb4bad
|
data/Gemfile.lock
CHANGED
@@ -5,6 +5,14 @@ class SmartIoC::BeanDefinitionsStorage
|
|
5
5
|
@collection = []
|
6
6
|
end
|
7
7
|
|
8
|
+
def clear_dependencies
|
9
|
+
@collection.each do |bd|
|
10
|
+
bd.dependencies.each do |dependency|
|
11
|
+
dependency.bean_definition = nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
8
16
|
# @param bean_definition [BeanDefinition]
|
9
17
|
def push(bean_definition)
|
10
18
|
existing_bd = @collection.detect do |bd|
|
@@ -58,12 +58,11 @@ class SmartIoC::BeanFactory
|
|
58
58
|
raise LoadRecursion.new(bean_definition)
|
59
59
|
end
|
60
60
|
|
61
|
-
|
62
|
-
beans_cache = init_bean_definition_cache(bean_definition)
|
61
|
+
beans_cache = init_bean_definition_cache(bean_definition)
|
63
62
|
|
64
|
-
autodetect_bean_definitions_for_dependencies(bean_definition
|
65
|
-
preload_beans(bean_definition,
|
66
|
-
load_bean(bean_definition,
|
63
|
+
autodetect_bean_definitions_for_dependencies(bean_definition)
|
64
|
+
preload_beans(bean_definition, beans_cache[bean_definition])
|
65
|
+
load_bean(bean_definition, beans_cache)
|
67
66
|
end
|
68
67
|
end
|
69
68
|
|
@@ -115,19 +114,17 @@ class SmartIoC::BeanFactory
|
|
115
114
|
end
|
116
115
|
end
|
117
116
|
|
118
|
-
def autodetect_bean_definitions_for_dependencies(bean_definition
|
117
|
+
def autodetect_bean_definitions_for_dependencies(bean_definition)
|
119
118
|
bean_definition.dependencies.each do |dependency|
|
120
|
-
next if
|
119
|
+
next if dependency.bean_definition
|
121
120
|
|
122
121
|
@bean_file_loader.require_bean(dependency.ref)
|
123
122
|
|
124
|
-
|
123
|
+
dependency.bean_definition = autodetect_bean_definition(
|
125
124
|
dependency.ref, dependency.package, bean_definition.package
|
126
125
|
)
|
127
126
|
|
128
|
-
|
129
|
-
|
130
|
-
autodetect_bean_definitions_for_dependencies(bd, dependency_cache)
|
127
|
+
autodetect_bean_definitions_for_dependencies(dependency.bean_definition)
|
131
128
|
end
|
132
129
|
end
|
133
130
|
|
@@ -174,7 +171,7 @@ class SmartIoC::BeanFactory
|
|
174
171
|
return smart_bds.first
|
175
172
|
end
|
176
173
|
|
177
|
-
def preload_beans(bean_definition,
|
174
|
+
def preload_beans(bean_definition, beans_cache)
|
178
175
|
scope = get_scope(bean_definition)
|
179
176
|
|
180
177
|
if scope_bean = scope.get_bean(bean_definition.klass)
|
@@ -184,13 +181,13 @@ class SmartIoC::BeanFactory
|
|
184
181
|
end
|
185
182
|
|
186
183
|
bean_definition.dependencies.each do |dependency|
|
187
|
-
bd =
|
184
|
+
bd = dependency.bean_definition
|
188
185
|
|
189
186
|
next if beans_cache[:dependencies].has_key?(bd)
|
190
187
|
|
191
188
|
dep_bean_cache = init_bean_definition_cache(bd)
|
192
189
|
beans_cache[:dependencies].merge!(dep_bean_cache)
|
193
|
-
preload_beans(bd,
|
190
|
+
preload_beans(bd, dep_bean_cache[bd])
|
194
191
|
end
|
195
192
|
end
|
196
193
|
|
@@ -229,8 +226,12 @@ class SmartIoC::BeanFactory
|
|
229
226
|
|
230
227
|
def init_zero_dep_factory_beans(beans_cache)
|
231
228
|
beans_cache.each do |bean_definition, bd_opts|
|
232
|
-
if bean_definition.has_factory_method?
|
233
|
-
|
229
|
+
if bean_definition.has_factory_method?
|
230
|
+
has_factory_dependencies = !!bean_definition.dependencies.detect {|dep| dep.bean_definition.has_factory_method?}
|
231
|
+
|
232
|
+
if bean_definition.dependencies.size == 0 || !has_factory_dependencies
|
233
|
+
init_factory_bean(bean_definition, bd_opts)
|
234
|
+
end
|
234
235
|
end
|
235
236
|
init_zero_dep_factory_beans(bd_opts[:dependencies]) if !bd_opts[:dependencies].empty?
|
236
237
|
end
|
@@ -247,44 +248,50 @@ class SmartIoC::BeanFactory
|
|
247
248
|
collection
|
248
249
|
end
|
249
250
|
|
250
|
-
def init_dependent_factory_beans(beans_cache
|
251
|
+
def init_dependent_factory_beans(beans_cache)
|
251
252
|
dependent_factory_beans = collect_dependent_factory_beans(beans_cache, [])
|
252
253
|
|
253
254
|
dependent_factory_beans.each do |bean_definition|
|
254
|
-
|
255
|
-
|
255
|
+
cross_refference_bd = get_cross_refference(dependent_factory_beans, bean_definition)
|
256
|
+
|
257
|
+
if cross_refference_bd
|
258
|
+
has_factory_dependencies = !!cross_refference_bd.dependencies.detect {|dep| dep.bean_definition.has_factory_method?}
|
259
|
+
|
260
|
+
if has_factory_dependencies
|
261
|
+
raise ArgumentError, "Factory method beans should not cross refference each other. Bean :#{bean_definition.name} cross refferences bean :#{cross_refference_bd.name}."
|
262
|
+
end
|
256
263
|
end
|
257
264
|
end
|
258
265
|
|
259
266
|
beans_cache.each do |bean_definition, bd_opts|
|
260
267
|
if bean_definition.has_factory_method? && bean_definition.dependencies.size > 0
|
261
|
-
inject_beans(bean_definition,
|
268
|
+
inject_beans(bean_definition, bd_opts)
|
262
269
|
init_factory_bean(bean_definition, bd_opts)
|
263
270
|
end
|
264
|
-
init_dependent_factory_beans(bd_opts[:dependencies]
|
271
|
+
init_dependent_factory_beans(bd_opts[:dependencies])
|
265
272
|
end
|
266
273
|
end
|
267
274
|
|
268
|
-
def load_bean(bean_definition,
|
275
|
+
def load_bean(bean_definition, beans_cache)
|
269
276
|
init_zero_dep_factory_beans(beans_cache)
|
270
|
-
init_dependent_factory_beans(beans_cache
|
271
|
-
inject_beans(bean_definition,
|
277
|
+
init_dependent_factory_beans(beans_cache)
|
278
|
+
inject_beans(bean_definition, beans_cache[bean_definition])
|
272
279
|
beans_cache[bean_definition][:scope_bean].bean
|
273
280
|
end
|
274
281
|
|
275
|
-
def inject_beans(bean_definition,
|
282
|
+
def inject_beans(bean_definition, beans_cache)
|
276
283
|
bean = beans_cache[:scope_bean].bean
|
277
284
|
bean_definition.dependencies.each do |dependency|
|
278
|
-
bd =
|
285
|
+
bd = dependency.bean_definition
|
279
286
|
dep_bean = beans_cache[:dependencies][bd][:scope_bean].bean
|
280
287
|
bean.instance_variable_set(:"@#{dependency.bean}", dep_bean)
|
281
|
-
inject_beans(bd,
|
288
|
+
inject_beans(bd, beans_cache[:dependencies][bd])
|
282
289
|
end
|
283
290
|
end
|
284
291
|
|
285
|
-
def get_cross_refference(refer_bean_definitions, current_bean_definition,
|
292
|
+
def get_cross_refference(refer_bean_definitions, current_bean_definition, seen_bean_definitions = [])
|
286
293
|
current_bean_definition.dependencies.each do |dependency|
|
287
|
-
bd =
|
294
|
+
bd = dependency.bean_definition
|
288
295
|
|
289
296
|
next if seen_bean_definitions.include?(bd)
|
290
297
|
|
@@ -292,7 +299,7 @@ class SmartIoC::BeanFactory
|
|
292
299
|
return bd
|
293
300
|
end
|
294
301
|
|
295
|
-
if crbd = get_cross_refference(refer_bean_definitions, bd,
|
302
|
+
if crbd = get_cross_refference(refer_bean_definitions, bd, seen_bean_definitions + [bd])
|
296
303
|
return crbd
|
297
304
|
end
|
298
305
|
end
|
data/lib/smart_ioc/container.rb
CHANGED
@@ -111,6 +111,7 @@ module SmartIoC
|
|
111
111
|
# @param context [Symbol] context (ex: :test)
|
112
112
|
def set_extra_context_for_package(package_name, context)
|
113
113
|
extra_package_contexts.set_context(package_name, context)
|
114
|
+
bean_definitions_storage.clear_dependencies
|
114
115
|
end
|
115
116
|
|
116
117
|
# @param bean_name [Symbol] bean name
|
data/lib/smart_ioc/version.rb
CHANGED
@@ -46,4 +46,46 @@ describe 'Factory Method' do
|
|
46
46
|
it 'assigns bean with factory method' do
|
47
47
|
expect(@other_service.test_config).to be_a(TestConfig::Config)
|
48
48
|
end
|
49
|
+
|
50
|
+
context 'cross refference factory method beans' do
|
51
|
+
before :all do
|
52
|
+
class SingletonBean
|
53
|
+
include SmartIoC::Iocify
|
54
|
+
|
55
|
+
bean :singleton_bean, package: :cross_refference
|
56
|
+
end
|
57
|
+
|
58
|
+
class FactoryConfig
|
59
|
+
include SmartIoC::Iocify
|
60
|
+
|
61
|
+
bean :factory_config, factory_method: :build, package: :cross_refference
|
62
|
+
|
63
|
+
inject :singleton_bean
|
64
|
+
|
65
|
+
def build
|
66
|
+
Object.new
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
class FactoryLogger
|
71
|
+
include SmartIoC::Iocify
|
72
|
+
|
73
|
+
bean :factory_logger, factory_method: :build, package: :cross_refference
|
74
|
+
|
75
|
+
inject :factory_config
|
76
|
+
|
77
|
+
def build
|
78
|
+
Object.new
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'creates factory_logger bean' do
|
84
|
+
expect(SmartIoC.get_bean(:factory_logger, package: :cross_refference)).to be_a(Object)
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'creates factory_config bean' do
|
88
|
+
expect(SmartIoC.get_bean(:factory_config, package: :cross_refference)).to be_a(Object)
|
89
|
+
end
|
90
|
+
end
|
49
91
|
end
|
@@ -34,7 +34,7 @@ describe SmartIoC do
|
|
34
34
|
it 'sets beans with extra package context' do
|
35
35
|
SmartIoC.set_extra_context_for_package(:admins, :test)
|
36
36
|
SmartIoC.force_clear_scopes
|
37
|
-
|
37
|
+
|
38
38
|
users_creator = @container.get_bean(:users_creator)
|
39
39
|
users_creator.create(1, 'test@test.com')
|
40
40
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_ioc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.28
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruslan Gatiyatov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|