collapsium 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/collapsium/recursive_merge.rb +26 -4
- data/lib/collapsium/version.rb +1 -1
- data/spec/recursive_merge_spec.rb +21 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e38a7e5022afa4e3f962c01a7e9c55a416a0d616
|
4
|
+
data.tar.gz: eab2d558811d1cc347684ea3c3754ad7cc00365b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db7393895094ff23e6c43dcc2de37da52f541482494c16d9dffde25205710fb9dccb86ccb9750705bcb395e7b7e43b9dcb2450189b3693751527137bda9adc43
|
7
|
+
data.tar.gz: 1a04a7bc768b18d8f27058005c074c383e65f7950ffe13d1d9245d509b364e6f57bfc26177c63e9e3f16fdcf423e086e5415398725582a4f6475f0f594834ba5
|
data/Gemfile.lock
CHANGED
@@ -49,6 +49,31 @@ module Collapsium
|
|
49
49
|
end
|
50
50
|
|
51
51
|
class << self
|
52
|
+
def merged_keys(the_self, v1, v2)
|
53
|
+
keys = (v1.keys + v2.keys).uniq
|
54
|
+
if the_self.singleton_class.ancestors.include?(IndifferentAccess)
|
55
|
+
# We want to preserve each Hash's key types as much as possible, but
|
56
|
+
# IndifferentAccess doesn't care about types. We can use it to figure out
|
57
|
+
# which unique keys only exist in v2.
|
58
|
+
only_v2 = IndifferentAccess.unique_keys(keys) \
|
59
|
+
- IndifferentAccess.unique_keys(v1.keys)
|
60
|
+
|
61
|
+
# At this point, IndifferentAccess may have modified the key types
|
62
|
+
# in only_v2. To get back the original types, we can iterate the
|
63
|
+
# Hash and remember all keys that are indifferently contained in
|
64
|
+
# only_v2.
|
65
|
+
original_types = []
|
66
|
+
v2.each do |key, _|
|
67
|
+
unique = IndifferentAccess.unique_keys([key])
|
68
|
+
if only_v2.include?(unique[0])
|
69
|
+
original_types << key
|
70
|
+
end
|
71
|
+
end
|
72
|
+
keys = v1.keys + original_types
|
73
|
+
end
|
74
|
+
return keys
|
75
|
+
end
|
76
|
+
|
52
77
|
def merger(the_self, v1, v2, overwrite)
|
53
78
|
if v1.is_a? Hash and v2.is_a? Hash
|
54
79
|
v1 = ViralCapabilities.enhance_value(the_self, v1)
|
@@ -56,10 +81,7 @@ module Collapsium
|
|
56
81
|
|
57
82
|
# IndifferentAccess has its own idea of which keys are unique, so if
|
58
83
|
# we use it, we must consult it.
|
59
|
-
keys = (v1
|
60
|
-
if the_self.singleton_class.ancestors.include?(IndifferentAccess)
|
61
|
-
keys = IndifferentAccess.unique_keys(keys)
|
62
|
-
end
|
84
|
+
keys = merged_keys(the_self, v1, v2)
|
63
85
|
new_val = ViralCapabilities.enhance_value(the_self, {})
|
64
86
|
keys.each do |key|
|
65
87
|
v1_inner = v1[key]
|
data/lib/collapsium/version.rb
CHANGED
@@ -147,6 +147,27 @@ describe ::Collapsium::RecursiveMerge do
|
|
147
147
|
expect(x[:arr].length).to eql 2
|
148
148
|
expect(x[:arr]).to eql [2, 1]
|
149
149
|
end
|
150
|
+
|
151
|
+
it "preserves key types, and prefers the first key's type" do
|
152
|
+
tester[:foo] = {
|
153
|
+
bar: 123,
|
154
|
+
}
|
155
|
+
tester[:arr] = [2]
|
156
|
+
other = {
|
157
|
+
"foo" => {
|
158
|
+
"baz asdf" => "quux",
|
159
|
+
"bar" => 42
|
160
|
+
},
|
161
|
+
"arr" => [1],
|
162
|
+
asdf: 42,
|
163
|
+
}
|
164
|
+
x = tester.recursive_merge(other, false)
|
165
|
+
|
166
|
+
expect(x.keys.length).to eql 3
|
167
|
+
expect(x.keys[0].is_a?(Symbol)).to be_truthy
|
168
|
+
expect(x.keys[1].is_a?(Symbol)).to be_truthy
|
169
|
+
expect(x.keys[2].is_a?(Symbol)).to be_truthy
|
170
|
+
end
|
150
171
|
end
|
151
172
|
|
152
173
|
context "with IndifferentAccess and PathedAccess" do
|