Skip to content

Commit a5bf11a

Browse files
committed
protected methods
1 parent cd1c6be commit a5bf11a

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
iqeo-conf (0.0.12)
4+
iqeo-conf (0.0.13)
55

66
GEM
77
remote: https://rubygems.org/

lib/iqeo/configuration.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def self.new_defer_block_for_parent parent, &block
4949
conf
5050
end
5151

52+
attr_accessor :_parent, :_items
53+
54+
# todo: defaults immediate children Configurations should have _parent updated
55+
5256
def initialize default = nil, &block
5357
@_items = case
5458
when default.kind_of?( HashWithIndifferentAccess ) then default
@@ -86,10 +90,8 @@ def method_missing name, *values, &block
8690
return _set name, values.first # set item to single value
8791
end
8892

89-
attr_accessor :_parent, :_items
90-
9193
def _set key, value
92-
# fix: extend parenting for enumerable with configurations at arbitrary depth
94+
# todo: extend parenting for enumerable with configurations at arbitrary depth
9395
case
9496
when value.kind_of?( Configuration ) then value._parent = self
9597
when value.kind_of?( Enumerable ) then value.each { |v| v._parent = self if v.kind_of? Configuration }
@@ -119,6 +121,8 @@ def _load file
119121
_read file.respond_to?(:read) ? file.read : File.read(file)
120122
end
121123

124+
# todo: merges should update _parent of any immediate child Configurations
125+
122126
def _merge! other
123127
@_items.merge! other._items
124128
self
@@ -128,8 +132,12 @@ def _merge other
128132
self.dup._merge! other
129133
end
130134

135+
# todo: can :_parent= be protected ?
136+
137+
protected :_parent, :_items, :_items=, :_get, :[], :_set, :[]=
131138

132139
end
133140

134141
end
135142

143+

spec/configuration_spec.rb

+36-32
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ def nested_configuration_example conf
249249
conf.alpha.bravo.charlie true
250250
end.to_not raise_error
251251
conf.should_not be_nil
252-
conf.alpha.bravo._parent.should be conf.alpha
253-
conf.alpha._parent.should be conf
252+
conf.alpha.bravo.send(:_parent).should be conf.alpha
253+
conf.alpha.send(:_parent).should be conf
254254
conf._parent.should be_nil
255255
end
256256

@@ -261,7 +261,7 @@ def nested_configuration_example conf
261261
conf.alpha Configuration.new, Configuration.new, Configuration.new
262262
end.to_not raise_error
263263
conf.should_not be_nil
264-
conf.alpha.each { |child| child._parent.should be conf }
264+
conf.alpha.each { |child| child.send(:_parent).should be conf }
265265
conf._parent.should be_nil
266266
end
267267

@@ -787,36 +787,40 @@ def nested_configuration_example conf
787787

788788
end # mode of usage
789789

790-
it 'can merge! configurations' do
791-
orig = simple_explicit_configuration
792-
orig.echo :original1
793-
orig.foxtrot :original2
794-
other = Configuration.new do
795-
foxtrot :overridden
796-
hotel :new
797-
end
798-
conf = orig._merge! other
799-
simple_configuration_example conf
800-
conf.echo.should be :original1
801-
conf.hotel.should be :new
802-
conf.foxtrot.should be :overridden
803-
conf.should be orig
804-
end
790+
context 'merge' do
791+
792+
it 'can update its own configuration' do
793+
orig = simple_explicit_configuration
794+
orig.echo :original1
795+
orig.foxtrot :original2
796+
other = Configuration.new do
797+
foxtrot :overridden
798+
hotel :new
799+
end
800+
conf = orig._merge! other
801+
simple_configuration_example conf
802+
conf.echo.should be :original1
803+
conf.hotel.should be :new
804+
conf.foxtrot.should be :overridden
805+
conf.should be orig
806+
end
807+
808+
it 'can create a new configuration' do
809+
orig = simple_explicit_configuration
810+
orig.echo :original1
811+
orig.foxtrot :original2
812+
other = Configuration.new do
813+
foxtrot :overridden
814+
hotel :new
815+
end
816+
conf = orig._merge other
817+
simple_configuration_example conf
818+
conf.echo.should be :original1
819+
conf.hotel.should be :new
820+
conf.foxtrot.should be :overridden
821+
conf.should_not be orig
822+
end
805823

806-
it 'can merge configurations' do
807-
orig = simple_explicit_configuration
808-
orig.echo :original1
809-
orig.foxtrot :original2
810-
other = Configuration.new do
811-
foxtrot :overridden
812-
hotel :new
813-
end
814-
conf = orig._merge other
815-
simple_configuration_example conf
816-
conf.echo.should be :original1
817-
conf.hotel.should be :new
818-
conf.foxtrot.should be :overridden
819-
conf.should_not be orig
820824
end
821825

822826
end

0 commit comments

Comments
 (0)