smart_ioc 0.1.29 → 0.1.30
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/smart_ioc/bean_factory.rb +30 -65
- data/lib/smart_ioc/version.rb +1 -1
- data/spec/smart_ioc/factory_method_spec.rb +16 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0146809a105a5135977a543956a5929d6dca9951
|
4
|
+
data.tar.gz: 51c86019fa338d8008bdf120dd17acc5be8e9f95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb68c1a7b8e4b7844ea8fb39f1cf7cb5711251d64e7b73516066b9aae85b3de013577ccfb2f742f136fb86d38318d6b72546862df4d2ae5e3c9a941110eddee6
|
7
|
+
data.tar.gz: 13b01014967f0c0677aca18cc06556e6a7a1bbc7265cb7e5f6aeb6e9165f6602112594b0f6876b3d10d94a08346abfb089df4c6ceaffc6a1c5ef5727609d0eb9
|
data/Gemfile.lock
CHANGED
@@ -66,6 +66,36 @@ class SmartIoC::BeanFactory
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
+
def load_bean(bean_definition, beans_cache)
|
70
|
+
bd_opts = beans_cache[bean_definition]
|
71
|
+
scope_bean = bd_opts[:scope_bean]
|
72
|
+
|
73
|
+
bean_definition.dependencies.each do |dependency|
|
74
|
+
bd = dependency.bean_definition
|
75
|
+
dep_db_opts = bd_opts[:dependencies][dependency.bean_definition]
|
76
|
+
dep_scope_bean = dep_db_opts[:scope_bean]
|
77
|
+
dep_bean = load_bean(bd, bd_opts[:dependencies])
|
78
|
+
|
79
|
+
scope_bean.bean.instance_variable_set(:"@#{dependency.bean}", dep_bean)
|
80
|
+
end
|
81
|
+
|
82
|
+
if !scope_bean.loaded
|
83
|
+
scope_bean.set_bean(scope_bean.bean.send(bean_definition.factory_method), true)
|
84
|
+
end
|
85
|
+
|
86
|
+
scope_bean.bean
|
87
|
+
end
|
88
|
+
|
89
|
+
def inject_beans(bean_definition, beans_cache)
|
90
|
+
bean = beans_cache[:scope_bean].bean
|
91
|
+
bean_definition.dependencies.each do |dependency|
|
92
|
+
bd = dependency.bean_definition
|
93
|
+
dep_bean = beans_cache[:dependencies][bd][:scope_bean].bean
|
94
|
+
bean.instance_variable_set(:"@#{dependency.bean}", dep_bean)
|
95
|
+
inject_beans(bd, beans_cache[:dependencies][bd])
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
69
99
|
def init_bean_definition_cache(bean_definition)
|
70
100
|
{
|
71
101
|
bean_definition => {
|
@@ -224,71 +254,6 @@ class SmartIoC::BeanFactory
|
|
224
254
|
end
|
225
255
|
end
|
226
256
|
|
227
|
-
def init_zero_dep_factory_beans(beans_cache)
|
228
|
-
beans_cache.each do |bean_definition, bd_opts|
|
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
|
235
|
-
end
|
236
|
-
init_zero_dep_factory_beans(bd_opts[:dependencies]) if !bd_opts[:dependencies].empty?
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
def collect_dependent_factory_beans(beans_cache, collection)
|
241
|
-
beans_cache.each do |bean_definition, bd_opts|
|
242
|
-
if bean_definition.has_factory_method? && bean_definition.dependencies.size > 0
|
243
|
-
collection << bean_definition
|
244
|
-
end
|
245
|
-
collect_dependent_factory_beans(bd_opts[:dependencies], collection)
|
246
|
-
end
|
247
|
-
|
248
|
-
collection
|
249
|
-
end
|
250
|
-
|
251
|
-
def init_dependent_factory_beans(beans_cache)
|
252
|
-
dependent_factory_beans = collect_dependent_factory_beans(beans_cache, [])
|
253
|
-
|
254
|
-
dependent_factory_beans.each do |bean_definition|
|
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
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
beans_cache.each do |bean_definition, bd_opts|
|
267
|
-
if bean_definition.has_factory_method? && bean_definition.dependencies.size > 0
|
268
|
-
inject_beans(bean_definition, bd_opts)
|
269
|
-
init_factory_bean(bean_definition, bd_opts)
|
270
|
-
end
|
271
|
-
init_dependent_factory_beans(bd_opts[:dependencies])
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
def load_bean(bean_definition, beans_cache)
|
276
|
-
init_zero_dep_factory_beans(beans_cache)
|
277
|
-
init_dependent_factory_beans(beans_cache)
|
278
|
-
inject_beans(bean_definition, beans_cache[bean_definition])
|
279
|
-
beans_cache[bean_definition][:scope_bean].bean
|
280
|
-
end
|
281
|
-
|
282
|
-
def inject_beans(bean_definition, beans_cache)
|
283
|
-
bean = beans_cache[:scope_bean].bean
|
284
|
-
bean_definition.dependencies.each do |dependency|
|
285
|
-
bd = dependency.bean_definition
|
286
|
-
dep_bean = beans_cache[:dependencies][bd][:scope_bean].bean
|
287
|
-
bean.instance_variable_set(:"@#{dependency.bean}", dep_bean)
|
288
|
-
inject_beans(bd, beans_cache[:dependencies][bd])
|
289
|
-
end
|
290
|
-
end
|
291
|
-
|
292
257
|
def get_cross_refference(refer_bean_definitions, current_bean_definition, seen_bean_definitions = [])
|
293
258
|
current_bean_definition.dependencies.each do |dependency|
|
294
259
|
bd = dependency.bean_definition
|
data/lib/smart_ioc/version.rb
CHANGED
@@ -54,6 +54,12 @@ describe 'Factory Method' do
|
|
54
54
|
|
55
55
|
bean :singleton_bean, package: :cross_refference
|
56
56
|
end
|
57
|
+
|
58
|
+
class OtherSingletonBean
|
59
|
+
include SmartIoC::Iocify
|
60
|
+
|
61
|
+
bean :other_singleton_bean, package: :cross_refference
|
62
|
+
end
|
57
63
|
|
58
64
|
class FactoryConfig
|
59
65
|
include SmartIoC::Iocify
|
@@ -61,6 +67,7 @@ describe 'Factory Method' do
|
|
61
67
|
bean :factory_config, factory_method: :build, package: :cross_refference
|
62
68
|
|
63
69
|
inject :singleton_bean
|
70
|
+
inject :other_singleton_bean
|
64
71
|
|
65
72
|
class Config
|
66
73
|
attr_reader :singleton_bean
|
@@ -81,23 +88,28 @@ describe 'Factory Method' do
|
|
81
88
|
bean :factory_logger, factory_method: :build, package: :cross_refference
|
82
89
|
|
83
90
|
inject :factory_config
|
91
|
+
inject :singleton_bean
|
84
92
|
|
85
93
|
class Logger
|
86
|
-
attr_reader :factory_config
|
94
|
+
attr_reader :factory_config, :singleton_bean
|
87
95
|
|
88
|
-
def initialize(factory_config)
|
96
|
+
def initialize(factory_config, singleton_bean)
|
89
97
|
@factory_config = factory_config
|
98
|
+
@singleton_bean = singleton_bean
|
90
99
|
end
|
91
100
|
end
|
92
101
|
|
93
102
|
def build
|
94
|
-
Logger.new(factory_config)
|
103
|
+
Logger.new(factory_config, singleton_bean)
|
95
104
|
end
|
96
105
|
end
|
97
106
|
end
|
98
107
|
|
99
108
|
it 'creates factory_logger bean' do
|
100
|
-
|
109
|
+
logger = SmartIoC.get_bean(:factory_logger, package: :cross_refference)
|
110
|
+
expect(logger).to be_a(FactoryLogger::Logger)
|
111
|
+
expect(logger.factory_config).to be_a(FactoryConfig::Config)
|
112
|
+
expect(logger.factory_config.singleton_bean).to be_a(SingletonBean)
|
101
113
|
end
|
102
114
|
|
103
115
|
it 'creates factory_config bean' do
|