Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be consistent with Hash#merge and Hash#merge! #27

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/deep_merge/deep_merge_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ def ko_deep_merge!(source, options = {})

# deep_merge! will merge and overwrite any unmergeables in destination hash
def deep_merge!(source, options = {})
default_opts = {:preserve_unmergeables => false}
default_opts = {:preserve_unmergeables => true}
DeepMerge::deep_merge!(source, self, default_opts.merge(options))
end

# deep_merge will merge and skip any unmergeables in destination hash
def deep_merge(source, options = {})
default_opts = {:preserve_unmergeables => true}
DeepMerge::deep_merge!(source, self, default_opts.merge(options))
copy = {}
DeepMerge::deep_merge!(self, copy, default_opts.merge(options))
DeepMerge::deep_merge!(source, copy, default_opts.merge(options))
end

end # DeepMergeHashExt
Expand Down
10 changes: 8 additions & 2 deletions test/test_deep_merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ def test_hash_deep_merge
assert hash_dest.deep_merge!(hash_src)
assert_equal({'id' => [1,2,3,4,5]}, hash_dest)

hash_src = {'id' => 'xxx'}
hash_src = {'id' => [3,4,5]}
hash_dest = {'id' => [1,2,3]}
assert hash_dest.deep_merge(hash_src)
assert_equal({'id' => [1,2,3,4,5]}, hash_dest.deep_merge(hash_src))
assert_equal({'id' => [1,2,3]}, hash_dest)

hash_src = {'id' => 'xxx'}
hash_dest = {'id' => [1,2,3]}
assert_equal({'id' => [1,2,3]}, hash_dest.deep_merge(hash_src))

assert({}.freeze.deep_merge(a: 1))
end

FIELD_KNOCKOUT_PREFIX = DeepMerge::DEFAULT_FIELD_KNOCKOUT_PREFIX
Expand Down