iqeo-conf 0.0.6 → 0.0.7

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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- iqeo-conf (0.0.6)
4
+ iqeo-conf (0.0.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,5 +1,5 @@
1
1
  module Iqeo
2
2
  class Configuration
3
- VERSION = "0.0.6"
3
+ VERSION = "0.0.7"
4
4
  end
5
5
  end
@@ -30,31 +30,48 @@ module Iqeo
30
30
  return self.read file.respond_to?(:read) ? file.read : File.read(file)
31
31
  end
32
32
 
33
+ def self.new_defer_block_for_parent parent, &block
34
+ conf = Configuration.new
35
+ conf._parent = parent
36
+ if block_given? && block.arity == 1
37
+ block.call(conf) # this is 'yield self' from the outside
38
+ else
39
+ raise "WTF! expected a block with a single parameter"
40
+ end
41
+ conf
42
+ end
43
+
33
44
  def initialize &block
34
45
  @items = HashWithIndifferentAccess.new
35
46
  @_parent = nil
36
47
  if block_given?
37
- if block.arity == 1
38
- yield self
39
- else
40
- instance_eval &block
41
- end
48
+ if block.arity == 1 # cannot set parent for yield blocks here as self is wrong !?
49
+ yield self
50
+ else
51
+ if block.binding.eval('self').kind_of?( Configuration ) # for eval block if nested configuration
52
+ @_parent = block.binding.eval('self') # set parent to make inherited values available
53
+ end # during block execution
54
+ instance_eval &block
55
+ end
42
56
  end
43
57
  end
44
58
 
45
59
  def method_missing name, *values, &block
46
- return @items.send name, *values if @items.respond_to? name # @items methods are highest priority
47
- # this is unreachable since these methods are delegated to @items hash
48
- # but keep it around for when we make selective delegation an option
49
- #case name
50
- #when :[]= then return _set values.shift, values.size > 1 ? values : values.first
51
- #when :[] then return _get values.shift
52
- #end
60
+ return @items.send name, *values if @items.respond_to? name # @items methods are highest priority
61
+
53
62
  name = name.to_s.chomp('=') # todo: write a test case for a non-string object as key being converted by .to_s
54
- return _set name, Configuration.new( &block ) if block_given? # block is a nested configuration
55
- return _get name if values.empty? # just get item
56
- return _set name, values if values.size > 1 # set item to multiple values
57
- return _set name, values.first # set item to single value
63
+
64
+ if block_given? # block is a nested configuration
65
+ if block.arity == 1 # yield DSL needs deferred block to set parent without binding
66
+ return _set name, Configuration.new_defer_block_for_parent( self, &block )
67
+ else
68
+ return _set name, Configuration.new( &block ) # eval DSL can set parent from block binding in initialize
69
+ end
70
+ end
71
+
72
+ return _get name if values.empty? # just get item
73
+ return _set name, values if values.size > 1 # set item to multiple values
74
+ return _set name, values.first # set item to single value
58
75
  end
59
76
 
60
77
  attr_accessor :_parent # todo: should attr_writer be protected ?
Binary file
@@ -373,6 +373,36 @@ describe Configuration do
373
373
  conf.bravo.delta.echo.should be_true
374
374
  end
375
375
 
376
+ it 'nested configuration can refer to an inherited setting' do
377
+ conf = nil
378
+ expect do
379
+ conf = Configuration.new do |c1|
380
+ c1.alpha true
381
+ c1.hotel c1.alpha
382
+ c1.bravo do |c2|
383
+ c2.charlie true
384
+ c2.foxtrot c2.alpha
385
+ c2.delta do |c3|
386
+ c3.echo true
387
+ c3.golf c3.alpha
388
+ end
389
+ end
390
+ end
391
+ end.to_not raise_error
392
+ conf.should_not be_nil
393
+ conf.alpha.should be_true
394
+ conf.bravo.should be_a Configuration
395
+ conf.bravo.alpha should be_true
396
+ conf.bravo.charlie should be_true
397
+ conf.bravo.delta.should be_a Configuration
398
+ conf.bravo.delta.alpha.should be_true
399
+ conf.bravo.delta.charlie.should be_true
400
+ conf.bravo.delta.echo.should be_true
401
+ conf.bravo.delta.golf.should be_true
402
+ conf.bravo.foxtrot.should be_true
403
+ conf.hotel.should be_true
404
+ end
405
+
376
406
  end # yield DSL
377
407
 
378
408
  context 'instance_eval DSL' do
@@ -470,6 +500,36 @@ describe Configuration do
470
500
  conf.bravo.delta.echo.should be_true
471
501
  end
472
502
 
503
+ it 'nested configuration can refer to an inherited setting' do
504
+ conf = nil
505
+ expect do
506
+ conf = Configuration.new do
507
+ alpha true
508
+ hotel alpha
509
+ bravo do
510
+ charlie true
511
+ foxtrot alpha
512
+ delta do
513
+ echo true
514
+ golf alpha
515
+ end
516
+ end
517
+ end
518
+ end.to_not raise_error
519
+ conf.should_not be_nil
520
+ conf.alpha.should be_true
521
+ conf.bravo.should be_a Configuration
522
+ conf.bravo.alpha should be_true
523
+ conf.bravo.charlie should be_true
524
+ conf.bravo.delta.should be_a Configuration
525
+ conf.bravo.delta.alpha.should be_true
526
+ conf.bravo.delta.charlie.should be_true
527
+ conf.bravo.delta.echo.should be_true
528
+ conf.bravo.delta.golf.should be_true
529
+ conf.bravo.foxtrot.should be_true
530
+ conf.hotel.should be_true
531
+ end
532
+
473
533
  end # instance_eval DSL
474
534
 
475
535
  end # mode of usage
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iqeo-conf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -68,6 +68,7 @@ files:
68
68
  - pkg/iqeo-conf-0.0.3.gem
69
69
  - pkg/iqeo-conf-0.0.4.gem
70
70
  - pkg/iqeo-conf-0.0.5.gem
71
+ - pkg/iqeo-conf-0.0.6.gem
71
72
  - spec/configuration_spec.rb
72
73
  - spec/spec_helper.rb
73
74
  homepage: http://iqeo.github.com/iqeo-conf