Permalink
Browse files

Merge Arrays with nil elements properly

  • Loading branch information...
1 parent 308cb1c commit edac8af6c30d461816e4d188dc9b5960692ede74 @kindkid committed Apr 10, 2012
Showing with 7 additions and 1 deletion.
  1. +1 −1 lib/deep_merge/core.rb
  2. +6 −0 test/test_deep_merge.rb
View
@@ -123,7 +123,7 @@ def self.deep_merge!(source, dest, options = {})
end
end
# if there's a naked knockout_prefix in source, that means we are to truncate dest
- if source.index(knockout_prefix)
+ if knockout_prefix && source.index(knockout_prefix)
dest = clear_or_nil(dest); source.delete(knockout_prefix)
end
if dest.kind_of?(Array)
View
@@ -201,6 +201,12 @@ def test_deep_merge
DeepMerge::deep_merge!(hash_src, hash_dst)
assert_equal({"property" => {"bedroom_count" => {2=>3, "king_bed" => [3]}, "bathroom_count" => ["1"]}}, hash_dst)
+ # 3 hash layers holding arrays of int, but source includes a nil in the array
+ hash_src = {"property" => {"bedroom_count" => {"king_bed" => [nil], "queen_bed" => [1, nil]}, "bathroom_count" => [nil, "1"]}}
+ hash_dst = {"property" => {"bedroom_count" => {"king_bed" => [2], "queen_bed" => [4]}, "bathroom_count" => ["2"]}}
+ DeepMerge::deep_merge!(hash_src, hash_dst)
+ assert_equal({"property" => {"bedroom_count" => {"king_bed" => [2,nil], "queen_bed" => [4, 1, nil]}, "bathroom_count" => ["2", nil, "1"]}}, hash_dst)
+
# test parameter management for knockout_prefix and overwrite unmergable
assert_raise(DeepMerge::InvalidParameter) {DeepMerge::deep_merge!(hash_src, hash_dst, {:knockout_prefix => ""})}
assert_raise(DeepMerge::InvalidParameter) {DeepMerge::deep_merge!(hash_src, hash_dst, {:preserve_unmergeables => true, :knockout_prefix => ""})}

0 comments on commit edac8af

Please sign in to comment.