micon 0.1.23 → 0.1.24

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/micon/core.rb +31 -20
  2. data/spec/config_spec.rb +6 -3
  3. metadata +1 -1
@@ -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
- @stack[key] = true
284
- o = (container && container[key]) || initializer.call
285
-
286
- if config
287
- apply_config o, config
288
- elsif config != false
289
- # Loading and caching config.
290
- config = get_config key
291
- config = false unless config # We use false to differentiate from nil.
292
- @metadata.initializers[key] = [initializer, dependencies, config]
293
-
294
- apply_config o, config if config
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
- if component.respond_to? :configure!
314
- component.configure! config
315
- else
316
- config.each{|k, v| component.send("#{k}=", v)}
317
- end
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
  #
@@ -10,10 +10,13 @@ describe "Configuration" do
10
10
  end
11
11
  end
12
12
 
13
- it "should allow to explicitly configure component if configure! method provided" do
13
+ it "should use custom configuration if we explicitly require config in initializer" do
14
14
  logger = Object.new
15
- logger.should_receive(:configure!).with(level: :info)
16
- micon.register(:logger){logger}
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]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.23
4
+ version: 0.1.24
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: