@@ -85,26 +85,38 @@ def self.expand(filename, obj, yaml_opts = {})
8585
8686 target_obj = expand ( filename , target_obj , yaml_opts )
8787 target_obj . each do |target_key , target_value |
88-
89- new_obj [ target_key ] = target_value
88+ if ( new_obj [ target_key ] . is_a? ( Hash ) )
89+ raise "Should be a hash" unless target_value . is_a? ( Hash )
90+ new_obj [ target_key ] = target_value . merge ( new_obj [ target_key ] )
91+ else
92+ new_obj [ target_key ] = target_value
93+ end
9094 end
9195 end
9296
9397 obj . delete ( "$mref" )
94- obj_keys = obj . keys
95- obj_keys . each do | key |
96- value = obj [ key ]
97-
98- expanded = expand ( filename , value , yaml_opts )
99- if new_obj [ key ] . is_a? ( Hash )
100- raise "should be a hash" unless expanded . is_a? ( Hash )
101- new_obj [ key ] . merge! ( expanded )
98+ # now merge target_obj and obj
99+ keys = ( obj . keys + new_obj . keys ) . uniq
100+ final_obj = { }
101+ keys . each do | key |
102+ if ! obj . key? ( key )
103+ final_obj [ key ] = new_obj [ key ]
104+ elsif ! new_obj . key? ( key )
105+ final_obj [ key ] = expand ( filename , obj [ key ] , yaml_opts )
102106 else
103- new_obj [ key ] = expanded
107+ value = obj [ key ]
108+
109+ if new_obj [ key ] . is_a? ( Hash )
110+ raise "should be a hash" unless new_obj [ key ] . is_a? ( Hash )
111+ final_obj [ key ] = new_obj [ key ] . merge ( obj [ key ] )
112+ else
113+ final_obj [ key ] = expand ( filename , obj [ key ] , yaml_opts )
114+ end
104115 end
105116 end
106117
107- new_obj
118+ final_obj
119+
108120 else
109121 obj_keys = obj . keys
110122 obj_keys . each do |key |
0 commit comments