collapsium 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99a1088fec0a9bbcf2a9d47fb75e7447076e2c62
4
- data.tar.gz: c0c3e428c35090a49f249beadff437fa6bda5bd4
3
+ metadata.gz: e38a7e5022afa4e3f962c01a7e9c55a416a0d616
4
+ data.tar.gz: eab2d558811d1cc347684ea3c3754ad7cc00365b
5
5
  SHA512:
6
- metadata.gz: c3336fb842483ffce622e2db745345cb2c3647fc17ba66d8b09ac7c082f88e4b538fcd72ced6b5c4614746a8a4c8c37b26fa363d07de3f425f9206b9b9434194
7
- data.tar.gz: d548be9a8b9a4cf527024ae41e6e8563decebcbd08929137fbb4998bcee40b59c970c2454d18a84474a3de44f5d883d3a8010de5470e049258033fda4e2dbb26
6
+ metadata.gz: db7393895094ff23e6c43dcc2de37da52f541482494c16d9dffde25205710fb9dccb86ccb9750705bcb395e7b7e43b9dcb2450189b3693751527137bda9adc43
7
+ data.tar.gz: 1a04a7bc768b18d8f27058005c074c383e65f7950ffe13d1d9245d509b364e6f57bfc26177c63e9e3f16fdcf423e086e5415398725582a4f6475f0f594834ba5
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- collapsium (0.8.1)
4
+ collapsium (0.8.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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.keys + v2.keys).uniq
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]
@@ -8,5 +8,5 @@
8
8
  #
9
9
  module Collapsium
10
10
  # The current release version
11
- VERSION = "0.8.1".freeze
11
+ VERSION = "0.8.2".freeze
12
12
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collapsium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Finkhaeuser