deep_merge 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +6 -0
- data/README.md +11 -0
- data/lib/deep_merge/core.rb +16 -2
- data/test/test_deep_merge.rb +19 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27ed5e6b4d266ffa2bd3b8dee28813b8325a3a5e
|
4
|
+
data.tar.gz: ae7c33aafbf1277c947f35562638470218612feb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f5c7e78852559834b2d72fa919c563af40dcf6d77b8e9e04f39128b59630d582fdee9d9544f0dfb2f2ccfa05b70ebb3deb3a2505ec0a56ff0c316e2930b792e
|
7
|
+
data.tar.gz: 8c3ac59055a9165e8e30b79df3800eca340bd781ecebe3c13592327c4e57dca1400ce7fd5e57497e88a40258e32183062a92074f517007762e1f292bf7554d07
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -39,6 +39,8 @@ Options are specified in the last parameter passed, which should be in hash form
|
|
39
39
|
Set to true to merge hashes within arrays
|
40
40
|
:extend_existing_arrays DEFAULT: false
|
41
41
|
Set to true to extend existing arrays, instead of overwriting them
|
42
|
+
:merge_nil_values DEFAULT: false
|
43
|
+
Set to true to merge nil hash values, overwriting a possibly non-nil value
|
42
44
|
:merge_debug DEFAULT: false
|
43
45
|
Set to true to get console output of merge process for debugging
|
44
46
|
|
@@ -99,6 +101,15 @@ Push src elements to existing arrays, instead of overwriting them.
|
|
99
101
|
dest.deep_merge!(source, {:extend_existing_arrays => true})
|
100
102
|
Results: {"property" => ["1", "2", "3", "4"]}
|
101
103
|
|
104
|
+
**:merge_nil_values**
|
105
|
+
|
106
|
+
The purpose of this option is to allow nil hash values to be merged. The prior behavior was to discard nil hash values and remains the default if not specified.
|
107
|
+
|
108
|
+
source = {"item" => nil}
|
109
|
+
dest = {"item" => "existing"}
|
110
|
+
dest.deep_merge!(source, {:merge_nil_values => true})
|
111
|
+
Results: {"item" => nil}
|
112
|
+
|
102
113
|
There are many tests for this library - and you can learn more about the features and usages of deep_merge! by just browsing the test examples.
|
103
114
|
|
104
115
|
Using deep_merge in Rails
|
data/lib/deep_merge/core.rb
CHANGED
@@ -34,6 +34,8 @@ module DeepMerge
|
|
34
34
|
# Set to string value to run "Array::join" then "String::split" against all arrays
|
35
35
|
# :merge_hash_arrays DEFAULT: false
|
36
36
|
# Set to true to merge hashes within arrays
|
37
|
+
# :keep_array_duplicates DEFAULT: false
|
38
|
+
# Set to true to preserve duplicate array entries
|
37
39
|
# :merge_debug DEFAULT: false
|
38
40
|
# Set to true to get console output of merge process for debugging
|
39
41
|
#
|
@@ -64,7 +66,13 @@ module DeepMerge
|
|
64
66
|
# dest = {:x => [{:z => 2}]}
|
65
67
|
# dest.deep_merge!(source, {:merge_hash_arrays => true})
|
66
68
|
# Results: {:x => [{:y => 1, :z => 2}]}
|
67
|
-
#
|
69
|
+
#
|
70
|
+
# :keep_array_duplicates => merges arrays within hashes but keeps duplicate elements
|
71
|
+
# source = {:x => {:y => [1,2,2,2,3]}}
|
72
|
+
# dest = {:x => {:y => [4,5,6]}}
|
73
|
+
# dest.deep_merge!(source, {:keep_array_duplicates => true})
|
74
|
+
# Results: {:x => {:y => [1,2,2,2,3,4,5,6]}}
|
75
|
+
#
|
68
76
|
# There are many tests for this library - and you can learn more about the features
|
69
77
|
# and usages of deep_merge! by just browsing the test examples
|
70
78
|
def self.deep_merge!(source, dest, options = {})
|
@@ -84,10 +92,14 @@ module DeepMerge
|
|
84
92
|
merge_hash_arrays = options[:merge_hash_arrays] || false
|
85
93
|
# request to extend existing arrays, instead of overwriting them
|
86
94
|
extend_existing_arrays = options[:extend_existing_arrays] || false
|
95
|
+
# request that arrays keep duplicate elements
|
96
|
+
keep_array_duplicates = options[:keep_array_duplicates] || false
|
97
|
+
# request that nil values are merged or skipped (Skipped/false by default)
|
98
|
+
merge_nil_values = options[:merge_nil_values] || false
|
87
99
|
|
88
100
|
di = options[:debug_indent] || ''
|
89
101
|
# do nothing if source is nil
|
90
|
-
return dest if source.nil?
|
102
|
+
return dest if !merge_nil_values && source.nil?
|
91
103
|
# if dest doesn't exist, then simply copy source to it
|
92
104
|
if !(dest) && overwrite_unmergeable
|
93
105
|
dest = source; return dest
|
@@ -168,6 +180,8 @@ module DeepMerge
|
|
168
180
|
end
|
169
181
|
list += source[dest.count..-1] if source.count > dest.count
|
170
182
|
dest = list
|
183
|
+
elsif keep_array_duplicates
|
184
|
+
dest = dest.concat(source)
|
171
185
|
else
|
172
186
|
dest = dest | source
|
173
187
|
end
|
data/test/test_deep_merge.rb
CHANGED
@@ -622,5 +622,24 @@ class TestDeepMerge < Test::Unit::TestCase
|
|
622
622
|
hash_src = {"item" => s2 }
|
623
623
|
DeepMerge::deep_merge!(hash_src, hash_dst)
|
624
624
|
assert_equal({"item" => ""}, hash_dst)
|
625
|
+
|
626
|
+
######################################
|
627
|
+
# tests for "keep_array_duplicates" option
|
628
|
+
hash_src = {"item" => ["2", "3"]}
|
629
|
+
hash_dst = {"item" => ["1", "2"]}
|
630
|
+
DeepMerge::deep_merge!(hash_src, hash_dst, {:keep_array_duplicates => true})
|
631
|
+
assert_equal({"item" => ["1", "2", "2", "3"]}, hash_dst)
|
632
|
+
|
633
|
+
# Don't merge nil values by default
|
634
|
+
hash_src = {"item" => nil}
|
635
|
+
hash_dst = {"item" => "existing"}
|
636
|
+
DeepMerge::deep_merge!(hash_src, hash_dst)
|
637
|
+
assert_equal({"item" => "existing"}, hash_dst)
|
638
|
+
|
639
|
+
# Merge nil values via an explicit: :merge_nil_values => true
|
640
|
+
hash_src = {"item" => nil}
|
641
|
+
hash_dst = {"item" => "existing"}
|
642
|
+
DeepMerge::deep_merge!(hash_src, hash_dst, {:merge_nil_values => true})
|
643
|
+
assert_equal({"item" => nil}, hash_dst)
|
625
644
|
end # test_deep_merge
|
626
645
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deep_merge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Midgley
|
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
75
|
version: '0'
|
76
76
|
requirements: []
|
77
77
|
rubyforge_project:
|
78
|
-
rubygems_version: 2.
|
78
|
+
rubygems_version: 2.6.11
|
79
79
|
signing_key:
|
80
80
|
specification_version: 4
|
81
81
|
summary: Merge Deeply Nested Hashes
|