blacklight_advanced_search 5.3.0 → 5.3.1
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/VERSION +1 -1
- data/lib/blacklight_advanced_search.rb +29 -34
- data/spec/lib/deep_merge_spec.rb +109 -31
- 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: d25fc6826f0edccf6e69175e09bd622ab304e21a
|
|
4
|
+
data.tar.gz: d64e54527b54fe2bae942c363f953771ad763112
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e8488838860c7746f5ab351ceed56ecb2fb0f5825f832a491342c1d563e137c98a01c78d12976c9358ff70653ff42dc7d85642bf6b82da6b88384f5b60c88238
|
|
7
|
+
data.tar.gz: cdb92d6f7b7f77f811211498118f0df17060d97177727427948a1e4e1ec3253b14a7bd736a68090645a71ebe6f30e75d9b641f6a8277cf326d43a93a423865c6
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
5.3.
|
|
1
|
+
5.3.1
|
|
@@ -11,40 +11,35 @@ module BlacklightAdvancedSearch
|
|
|
11
11
|
|
|
12
12
|
require 'blacklight_advanced_search/version'
|
|
13
13
|
require 'blacklight_advanced_search/engine'
|
|
14
|
-
|
|
15
|
-
# Utility method used in our solr search logic.
|
|
16
|
-
# Merges new_hash into source_hash, but will recursively
|
|
17
|
-
# merge nested arrays and hashes too; also will NOT merge nil
|
|
18
|
-
# or blank values from new_hash into source_hash, nil or blank values
|
|
19
|
-
# in new_hash will not overwrite values in source_hash.
|
|
20
|
-
def self.deep_merge!(source_hash, new_hash)
|
|
21
|
-
# We used to use built-in source_hash.merge() with a block arg
|
|
22
|
-
# to customize merge behavior, but that was breaking in some
|
|
23
|
-
# versions of BL/Rails where source_hash was a kind of HashWithIndifferentAccess,
|
|
24
|
-
# and hwia is unreliable in some versions of Rails. Oh well.
|
|
25
|
-
# https://github.com/projectblacklight/blacklight/issues/827
|
|
26
14
|
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
# Utility method used in our solr search logic.
|
|
16
|
+
# Like Rails Hash#deep_merge, merges 2 hashes recursively, including nested Arrays and Hashes.
|
|
17
|
+
# Unlike Rails Hash#deep_merge:
|
|
18
|
+
# - will NOT merge nil values over existing ones
|
|
19
|
+
# - will NOT merge (non-FalseClass) blank values
|
|
20
|
+
# - WILL deduplicate values from arrays after merging them
|
|
21
|
+
#
|
|
22
|
+
# @param [Hash|HashWithIndifferentAccess] source_hash
|
|
23
|
+
# @param [Hash|HashWithIndifferentAccess] new_hash
|
|
24
|
+
# @return [Hash] the deeply merged hash
|
|
25
|
+
# @see Rails #deep_merge http://apidock.com/rails/v4.2.1/Hash/deep_merge
|
|
26
|
+
# @example new_hash = BlacklightAdvancedSearch.deep_merge(h1, h2)
|
|
27
|
+
def self.deep_merge(source_hash, new_hash)
|
|
28
|
+
source_hash.deep_merge(new_hash, &method(:merge_conflict_resolution))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# this one side-effects the first param
|
|
32
|
+
# @see #deep_merge
|
|
33
|
+
# @deprecated use `new_hash = BlacklightAdvancedSearch.deep_merge(h1, h2)` instead
|
|
34
|
+
def self.deep_merge!(source_hash, new_hash)
|
|
35
|
+
source_hash.deep_merge!(new_hash, &method(:merge_conflict_resolution))
|
|
36
|
+
end
|
|
29
37
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
old.concat(new_value).uniq
|
|
38
|
-
elsif new_value.nil?
|
|
39
|
-
# Allowing nil values to over-write on merge messes things up.
|
|
40
|
-
# don't set a nil value if you really want to force blank, set
|
|
41
|
-
# empty string.
|
|
42
|
-
old
|
|
43
|
-
else
|
|
44
|
-
new_value
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
source_hash
|
|
48
|
-
end
|
|
49
|
-
|
|
38
|
+
# the arguments are set by what the Rails Hash.deep_merge supplies the block
|
|
39
|
+
def self.merge_conflict_resolution(_key, old, new_value)
|
|
40
|
+
return old if new_value.nil?
|
|
41
|
+
return old if new_value.respond_to?(:blank?) && new_value.blank? && !new_value.is_a?(FalseClass)
|
|
42
|
+
return old | new_value if old.is_a?(Array) && new_value.is_a?(Array)
|
|
43
|
+
new_value
|
|
44
|
+
end
|
|
50
45
|
end
|
data/spec/lib/deep_merge_spec.rb
CHANGED
|
@@ -1,45 +1,123 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
|
-
describe
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
describe 'BlacklightAdvancedSearch#deep_merge' do
|
|
4
|
+
let(:hash_X) do
|
|
5
|
+
{
|
|
6
|
+
'a' => 'a',
|
|
7
|
+
'b' => 'b',
|
|
8
|
+
'array1' => [1, 2],
|
|
9
|
+
'array2' => [3, 4],
|
|
10
|
+
'hash1' => { 'a' => 'a', 'array' => [1], 'b' => 'b' },
|
|
11
|
+
'hash2' => { 'a2' => 'a2', 'array2' => [12], 'b2' => 'b2' }
|
|
12
|
+
}
|
|
13
|
+
end
|
|
14
|
+
let(:hash_Y) do
|
|
15
|
+
{
|
|
16
|
+
'a' => 'NEW A',
|
|
17
|
+
'c' => 'NEW C',
|
|
18
|
+
'array1' => [3, 4],
|
|
19
|
+
'hash1' => { 'array' => [2], 'b' => 'NEW B' }
|
|
9
20
|
}
|
|
10
|
-
|
|
11
|
-
BlacklightAdvancedSearch.deep_merge!(@ahash, {
|
|
12
|
-
"a" => "NEW A",
|
|
13
|
-
"array1" => [3, 4],
|
|
14
|
-
"hash1" => {
|
|
15
|
-
"array" => [2],
|
|
16
|
-
"b" => "NEW B"
|
|
17
|
-
},
|
|
18
|
-
"c" => "NEW C"
|
|
19
|
-
})
|
|
20
21
|
end
|
|
22
|
+
let(:ahash) do
|
|
23
|
+
BlacklightAdvancedSearch.deep_merge(hash_x, hash_y)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
RSpec.shared_examples 'Mergable Parameters' do # this name referenced below
|
|
27
|
+
it 'does not modify the param hashes' do
|
|
28
|
+
dup_x = hash_x.dup
|
|
29
|
+
dup_y = hash_y.dup
|
|
30
|
+
expect(ahash).not_to eq hash_x # this was the old behavior
|
|
31
|
+
expect(dup_x).to eq hash_x
|
|
32
|
+
expect(dup_y).to eq hash_y
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it 'leaves un-collided content alone' do
|
|
36
|
+
expect(ahash['b']).to eq('b')
|
|
37
|
+
expect(ahash['array2']).to eq([3, 4])
|
|
38
|
+
expect(ahash['hash2']).to eq('a2' => 'a2', 'array2' => [12], 'b2' => 'b2')
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'adds new content' do
|
|
42
|
+
expect(ahash['c']).to eq('NEW C')
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'merges a hash, recursive like' do
|
|
46
|
+
expect(ahash['hash1']).to eq('a' => 'a', 'array' => [1, 2], 'b' => 'NEW B')
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'merges boolean values (false)' do
|
|
50
|
+
expect(BlacklightAdvancedSearch.deep_merge({ a: false }, a: true)).to eq(a: true)
|
|
51
|
+
expect(BlacklightAdvancedSearch.deep_merge({ a: true }, a: false)).to eq(a: false)
|
|
52
|
+
end
|
|
21
53
|
|
|
54
|
+
it 'does not merge nil values over existing keys' do
|
|
55
|
+
expect(BlacklightAdvancedSearch.deep_merge({ a: 1 }, a: nil)).to eq(a: 1)
|
|
56
|
+
end
|
|
22
57
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
58
|
+
it 'does merge nil values when the key is not yet present' do
|
|
59
|
+
expect(BlacklightAdvancedSearch.deep_merge({}, a: nil)).to eq(a: nil)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it 'does not merge empty strings over existing keys' do
|
|
63
|
+
expect(BlacklightAdvancedSearch.deep_merge({ a: 1 }, a: '')).to eq(a: 1)
|
|
64
|
+
expect(BlacklightAdvancedSearch.deep_merge({ a: nil }, a: '')).to eq(a: nil)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it 'does not merge empty strings when the key is not yet present' do
|
|
68
|
+
expect(BlacklightAdvancedSearch.deep_merge({}, a: '')).to eq(a: '')
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
context 'Arrays' do
|
|
72
|
+
it 'merges an array' do
|
|
73
|
+
expect(ahash['array1']).to eq([1, 2, 3, 4])
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it 'collapse to uniq values when merging' do
|
|
77
|
+
expect(BlacklightAdvancedSearch.deep_merge({ a: [1, 1, 2, 1] }, a: [3, 2])).to eq(a: [1, 2, 3])
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it 'does not collapse to uniq values if not merging' do
|
|
81
|
+
expect(BlacklightAdvancedSearch.deep_merge({ a: [1, 1, 2, 1] }, a: [])).to eq(a: [1, 1, 2, 1])
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
describe Hash do
|
|
87
|
+
it_behaves_like 'Mergable Parameters' do
|
|
88
|
+
let(:hash_x) { hash_X }
|
|
89
|
+
let(:hash_y) { hash_Y }
|
|
90
|
+
end
|
|
27
91
|
end
|
|
28
92
|
|
|
29
|
-
|
|
30
|
-
|
|
93
|
+
describe HashWithIndifferentAccess do
|
|
94
|
+
it_behaves_like 'Mergable Parameters' do
|
|
95
|
+
let(:hash_x) { hash_X.with_indifferent_access }
|
|
96
|
+
let(:hash_y) { hash_Y.with_indifferent_access }
|
|
97
|
+
end
|
|
31
98
|
end
|
|
32
99
|
|
|
33
|
-
|
|
34
|
-
|
|
100
|
+
describe 'Mixed Hash and HWIA' do
|
|
101
|
+
it_behaves_like 'Mergable Parameters' do
|
|
102
|
+
let(:hash_x) { hash_X }
|
|
103
|
+
let(:hash_y) { hash_Y.with_indifferent_access }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it_behaves_like 'Mergable Parameters' do
|
|
107
|
+
let(:hash_x) { hash_X.with_indifferent_access }
|
|
108
|
+
let(:hash_y) { hash_Y }
|
|
109
|
+
end
|
|
35
110
|
end
|
|
36
111
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
112
|
+
# from http://apidock.com/rails/v4.2.1/Hash/deep_merge
|
|
113
|
+
describe 'reference example' do
|
|
114
|
+
it 'gives the same result as Rails Hash .deep_merge' do
|
|
115
|
+
h1 = { a: true, b: { c: [1, 2, 3] } }
|
|
116
|
+
h2 = { a: false, b: { x: [3, 4, 5] } }
|
|
117
|
+
merged = { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
|
|
118
|
+
expect(h1.deep_merge(h2)).to eq(merged)
|
|
119
|
+
expect(BlacklightAdvancedSearch.deep_merge(h1, h2)).to eq(merged)
|
|
120
|
+
end
|
|
43
121
|
end
|
|
44
122
|
|
|
45
|
-
end
|
|
123
|
+
end
|