micon 0.1.23 → 0.1.24
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.
- data/lib/micon/core.rb +31 -20
- data/spec/config_spec.rb +6 -3
- metadata +1 -1
data/lib/micon/core.rb
CHANGED
@@ -277,23 +277,34 @@ class Micon::Core
|
|
277
277
|
dependencies.each{|d| self[d]}
|
278
278
|
@metadata.call_before key
|
279
279
|
|
280
|
+
# We need this check to detect and prevent component from been used before its initialization
|
281
|
+
# is finished.
|
282
|
+
@stack[key] = true
|
283
|
+
|
280
284
|
# We need to check container first, in complex cases (circullar dependency)
|
281
285
|
# the object already may be initialized.
|
282
286
|
# See "should allow to use circullar dependency in :after callback".
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
287
|
+
o = (container && container[key]) || begin
|
288
|
+
# Loading component configuration, comparing to nil is important,
|
289
|
+
# we use false if there's no config.
|
290
|
+
if config == nil
|
291
|
+
config = get_config key
|
292
|
+
config = false unless config
|
293
|
+
@metadata.initializers[key] = [initializer, dependencies, config]
|
294
|
+
end
|
295
|
+
|
296
|
+
# Use arity to determine should we apply cofig automatically or
|
297
|
+
# component wants to apply it by themself.
|
298
|
+
if initializer.arity == 1
|
299
|
+
initializer.call config
|
300
|
+
else
|
301
|
+
o = initializer.call
|
302
|
+
config.each{|k, v| o.send("#{k}=", v)} if config
|
303
|
+
o
|
304
|
+
end
|
295
305
|
end
|
296
306
|
|
307
|
+
# Storing created component in container.
|
297
308
|
container[key] = o if container
|
298
309
|
|
299
310
|
raise "initializer for component :#{key} returns nill!" unless o
|
@@ -309,14 +320,14 @@ class Micon::Core
|
|
309
320
|
::Micon::Config.new(self, key).load
|
310
321
|
end
|
311
322
|
|
312
|
-
def apply_config component, config
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
end
|
319
|
-
|
323
|
+
# def apply_config component, config
|
324
|
+
# if component.respond_to? :configure!
|
325
|
+
# component.configure! config
|
326
|
+
# else
|
327
|
+
# config.each{|k, v| component.send("#{k}=", v)}
|
328
|
+
# end
|
329
|
+
# end
|
330
|
+
#
|
320
331
|
# Module.name doesn't works correctly for Anonymous classes,
|
321
332
|
# try to execute this code:
|
322
333
|
#
|
data/spec/config_spec.rb
CHANGED
@@ -10,10 +10,13 @@ describe "Configuration" do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
it "should
|
13
|
+
it "should use custom configuration if we explicitly require config in initializer" do
|
14
14
|
logger = Object.new
|
15
|
-
logger.should_receive(:
|
16
|
-
micon.register
|
15
|
+
logger.should_receive(:do_custom_initialization).with(level: :info)
|
16
|
+
micon.register :logger do |config|
|
17
|
+
logger.do_custom_initialization config
|
18
|
+
logger
|
19
|
+
end
|
17
20
|
|
18
21
|
with_load_path "#{spec_dir}/basic/lib" do
|
19
22
|
micon[:logger]
|