deep_merge 1.1.0 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG +23 -0
- data/README.md +38 -6
- data/lib/deep_merge/core.rb +21 -2
- data/test/test_deep_merge.rb +25 -0
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5e190853b33fb89d42881c6615a17c4fc50757ab261bae87e2639d553cf07dbe
|
4
|
+
data.tar.gz: 3c2d1e368cbc141220ab2a1c2ec7a346d085f7d6f3306f34da16027f08ef6825
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c71ee77b10d24d27d0814d5d23441855b6e1fc226a842f734919622e707aaffa0063e7cdaec7b1591bf5d6a0a1910260b727462074f655cb3a486419d9086165
|
7
|
+
data.tar.gz: 7ed27c1eaa73425856fedc0379c0ecec51d6636ea5abe5f9cf2c48e7d7f36f7ffafc579eca8b3d1b42486a6e2f27ceb2cd99bdfbb27df3d7596f13ad7543482e
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,26 @@
|
|
1
|
+
2022-01-07 Jason Frey <fryguy9@gmail.com>
|
2
|
+
* Ship version 1.2.2
|
3
|
+
|
4
|
+
* Switched to GitHub actions and add testing of Rubies up to Ruby 3.0
|
5
|
+
* Fixed issue with keep_duplicate_arrays when merging into a nil array. Thanks ALTinners!
|
6
|
+
* Added documentation for keep_duplicate_arrays
|
7
|
+
|
8
|
+
2017-11-16 Jason Frey <fryguy9@gmail.com>
|
9
|
+
* Ship version 1.2.1
|
10
|
+
|
11
|
+
* Fixed release date in the gemspec.
|
12
|
+
|
13
|
+
2017-11-16 Jason Frey <fryguy9@gmail.com>
|
14
|
+
* Ship version 1.2.0
|
15
|
+
|
16
|
+
2017-04-25 Joe Rafaniello <jrafanie@redhat.com>
|
17
|
+
* Merge nil values or keep the original via an option
|
18
|
+
|
19
|
+
2016-08-01 Jason Frey <fryguy9@gmail.com>
|
20
|
+
* Ship version 1.1.1
|
21
|
+
|
22
|
+
* Fixed release date in the gemspec.
|
23
|
+
|
1
24
|
2016-08-01 Jason Frey <fryguy9@gmail.com>
|
2
25
|
* Ship version 1.1.0
|
3
26
|
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
-
DeepMerge
|
2
|
-
|
1
|
+
DeepMerge
|
2
|
+
=========
|
3
|
+
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/deep_merge.svg)](http://badge.fury.io/rb/deep_merge)
|
5
|
+
[![CI](https://github.com/danielsdeleo/deep_merge/actions/workflows/ci.yaml/badge.svg)](https://github.com/danielsdeleo/deep_merge/actions/workflows/ci.yaml)
|
3
6
|
|
4
7
|
Deep Merge is a simple set of utility functions for Hash. It permits you to merge elements inside a hash together recursively. The manner by which it does this is somewhat arbitrary (since there is no defining standard for this) but it should end up being pretty intuitive and do what you expect.
|
5
8
|
|
@@ -39,6 +42,10 @@ Options are specified in the last parameter passed, which should be in hash form
|
|
39
42
|
Set to true to merge hashes within arrays
|
40
43
|
:extend_existing_arrays DEFAULT: false
|
41
44
|
Set to true to extend existing arrays, instead of overwriting them
|
45
|
+
:keep_array_duplicates DEFAULT: false
|
46
|
+
Set to true to keep duplicate entries in arrays, instead of coalescing them
|
47
|
+
:merge_nil_values DEFAULT: false
|
48
|
+
Set to true to merge nil hash values, overwriting a possibly non-nil value
|
42
49
|
:merge_debug DEFAULT: false
|
43
50
|
Set to true to get console output of merge process for debugging
|
44
51
|
|
@@ -62,7 +69,6 @@ Additionally, if the knockout_prefix is passed alone as a string, it will cause
|
|
62
69
|
Results: {:x => ""}
|
63
70
|
|
64
71
|
**:overwrite_arrays**
|
65
|
-
|
66
72
|
The purpose of this is to provide a way to replace Arrays instead of having them merge together.
|
67
73
|
|
68
74
|
source = {:x => ['1', '2']}
|
@@ -71,7 +77,6 @@ The purpose of this is to provide a way to replace Arrays instead of having them
|
|
71
77
|
Results: {:x => ['1', '2']}
|
72
78
|
|
73
79
|
**:unpack_arrays**
|
74
|
-
|
75
80
|
The purpose of this is to permit compound elements to be passed in as strings and to be converted into discrete array elements
|
76
81
|
|
77
82
|
irsource = {:x => ['1,2,3', '4']}
|
@@ -99,6 +104,33 @@ Push src elements to existing arrays, instead of overwriting them.
|
|
99
104
|
dest.deep_merge!(source, {:extend_existing_arrays => true})
|
100
105
|
Results: {"property" => ["1", "2", "3", "4"]}
|
101
106
|
|
107
|
+
**:keep_array_duplicates**
|
108
|
+
|
109
|
+
Keeps duplicate entries in arrays, instead of coalescing them.
|
110
|
+
|
111
|
+
Without this setting:
|
112
|
+
|
113
|
+
source = { "property" => ["2", "3"] }
|
114
|
+
dest = { "property" => ["1", "2"] }
|
115
|
+
dest.deep_merge!(source)
|
116
|
+
Results: {"property" => ["1", "2", "3"]}
|
117
|
+
|
118
|
+
With this setting:
|
119
|
+
|
120
|
+
source = { "property" => ["1", "2"] }
|
121
|
+
dest = { "property" => ["2", "3"] }
|
122
|
+
dest.deep_merge!(source, {:keep_array_duplicates => true})
|
123
|
+
Results: {"property" => ["1", "2", "2", "3"]}
|
124
|
+
|
125
|
+
**:merge_nil_values**
|
126
|
+
|
127
|
+
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.
|
128
|
+
|
129
|
+
source = {"item" => nil}
|
130
|
+
dest = {"item" => "existing"}
|
131
|
+
dest.deep_merge!(source, {:merge_nil_values => true})
|
132
|
+
Results: {"item" => nil}
|
133
|
+
|
102
134
|
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
135
|
|
104
136
|
Using deep_merge in Rails
|
@@ -127,8 +159,8 @@ Simple Example Code
|
|
127
159
|
y.deep_merge!(x)
|
128
160
|
# results: y = {:x => [1,2,3,4,5]}
|
129
161
|
|
130
|
-
|
131
|
-
|
162
|
+
Availability
|
163
|
+
============
|
132
164
|
|
133
165
|
`deep_merge` was written by Steve Midgley, and is now maintained by Daniel DeLeo. The official home of `deep_merge` on the internet is now https://github.com/danielsdeleo/deep_merge
|
134
166
|
|
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
|
@@ -110,6 +122,11 @@ module DeepMerge
|
|
110
122
|
rescue TypeError
|
111
123
|
src_dup = src_value
|
112
124
|
end
|
125
|
+
if src_dup.kind_of?(Array) && keep_array_duplicates
|
126
|
+
# note: in this case the merge will be additive, rather than a bounded set, so we can't simply merge src with itself
|
127
|
+
# We need to merge src with an empty array
|
128
|
+
src_dup = []
|
129
|
+
end
|
113
130
|
dest[src_key] = deep_merge!(src_value, src_dup, options.merge(:debug_indent => di + ' '))
|
114
131
|
end
|
115
132
|
elsif dest.kind_of?(Array) && extend_existing_arrays
|
@@ -168,6 +185,8 @@ module DeepMerge
|
|
168
185
|
end
|
169
186
|
list += source[dest.count..-1] if source.count > dest.count
|
170
187
|
dest = list
|
188
|
+
elsif keep_array_duplicates
|
189
|
+
dest = dest.concat(source)
|
171
190
|
else
|
172
191
|
dest = dest | source
|
173
192
|
end
|
data/test/test_deep_merge.rb
CHANGED
@@ -622,5 +622,30 @@ 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
|
+
# For Issue 34 - keep_array_duplicates against a nil src doesn't do a recursive merge
|
634
|
+
hash_src = {"item" => ["2", "3"]}
|
635
|
+
hash_dst = { }
|
636
|
+
DeepMerge::deep_merge!(hash_src, hash_dst, {:keep_array_duplicates => true})
|
637
|
+
assert_equal({"item" => ["2", "3"]}, hash_dst)
|
638
|
+
|
639
|
+
# Don't merge nil values by default
|
640
|
+
hash_src = {"item" => nil}
|
641
|
+
hash_dst = {"item" => "existing"}
|
642
|
+
DeepMerge::deep_merge!(hash_src, hash_dst)
|
643
|
+
assert_equal({"item" => "existing"}, hash_dst)
|
644
|
+
|
645
|
+
# Merge nil values via an explicit: :merge_nil_values => true
|
646
|
+
hash_src = {"item" => nil}
|
647
|
+
hash_dst = {"item" => "existing"}
|
648
|
+
DeepMerge::deep_merge!(hash_src, hash_dst, {:merge_nil_values => true})
|
649
|
+
assert_equal({"item" => nil}, hash_dst)
|
625
650
|
end # test_deep_merge
|
626
651
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Midgley
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -59,7 +59,7 @@ homepage: https://github.com/danielsdeleo/deep_merge
|
|
59
59
|
licenses:
|
60
60
|
- MIT
|
61
61
|
metadata: {}
|
62
|
-
post_install_message:
|
62
|
+
post_install_message:
|
63
63
|
rdoc_options: []
|
64
64
|
require_paths:
|
65
65
|
- lib
|
@@ -74,9 +74,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
requirements: []
|
77
|
-
|
78
|
-
|
79
|
-
signing_key:
|
77
|
+
rubygems_version: 3.1.6
|
78
|
+
signing_key:
|
80
79
|
specification_version: 4
|
81
80
|
summary: Merge Deeply Nested Hashes
|
82
81
|
test_files:
|