diff --git a/hashdiff.gemspec b/hashdiff.gemspec index c50bd0d..53e6650 100644 --- a/hashdiff.gemspec +++ b/hashdiff.gemspec @@ -22,4 +22,5 @@ Gem::Specification.new do |s| s.add_development_dependency("rspec", "~> 2.0") s.add_development_dependency("yard") s.add_development_dependency("bluecloth") + s.add_development_dependency("pry") end diff --git a/lib/hashdiff/util.rb b/lib/hashdiff/util.rb index bbb23ce..8efdaec 100644 --- a/lib/hashdiff/util.rb +++ b/lib/hashdiff/util.rb @@ -74,7 +74,7 @@ def self.decode_property_path(path, delimiter='.') def self.node(hash, parts) temp = hash parts.each do |part| - temp = temp[part] + temp = (temp[part] ||= {}) end temp end diff --git a/spec/hashdiff/patch_spec.rb b/spec/hashdiff/patch_spec.rb index 8f4df82..7970bf4 100644 --- a/spec/hashdiff/patch_spec.rb +++ b/spec/hashdiff/patch_spec.rb @@ -180,4 +180,12 @@ b = {"a" => 5, :a => 6, 0 => 7} HashDiff.unpatch!(b, diff).should == a end + + it "should auto create empty hash. BUG: cannot convert array" do + a = {} + HashDiff.patch!(a, [['+', 'a.b.c', 'a'], ['+', 'a.b.d', 'b'], ['+', 'b.c[1]', 'c']]).should == { + 'a' => { 'b' => { 'c' => 'a', 'd' => 'b'} }, + 'b' => { 'c' => { 1 => 'c' } } + } + end end