Skip to content

Commit 60fcabd

Browse files
committed
Make deep_merge a soft-requirement
The goal here is to remove deep_merge and rely entirely on ActiveSupport's `Hash#deep_merge` implementation. This does mean that if you want to keep the existing behavior, or rely on DeepMerge's specific options, you need to add the following to your Gemfile: ```ruby gem 'deep_merge', '~> 1.2', '>= 1.2.1' ```
1 parent 25bc414 commit 60fcabd

File tree

6 files changed

+90
-20
lines changed

6 files changed

+90
-20
lines changed

config.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
2727
s.require_paths = ['lib']
2828
s.required_ruby_version = '>= 2.6.0'
2929

30-
s.add_dependency 'deep_merge', '~> 1.2', '>= 1.2.1'
30+
#s.add_dependency 'deep_merge', '~> 1.2', '>= 1.2.1'
3131
s.add_dependency 'ostruct'
3232

3333
s.add_development_dependency 'dry-validation', *Config::DryValidationRequirements::VERSIONS

gemfiles/sinatra.gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
source "https://rubygems.org"
44

55
gem "sinatra", "2.0.8.1"
6+
gem "deep_merge", "~> 1.2", ">= 1.2.1"
67

78
gemspec path: "../"

lib/config.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
require 'config/sources/hash_source'
77
require 'config/sources/env_source'
88
require 'config/validation/schema'
9-
require 'deep_merge/core'
9+
10+
begin
11+
gem 'deep_merge', '~> 1.2', '>= 1.2.1'
12+
require 'deep_merge/core'
13+
rescue LoadError
14+
warn <<~WARNING
15+
The `deep_merge` gem is not available. Trying to use ActiveSupport's Hash#deep_merge instead.
16+
If you want to continue using 'deep_merge' gem, please add it to your Gemfile:
17+
gem 'deep_merge', '~> 1.2', '>= 1.2.1'
18+
WARNING
19+
require 'active_support/core_ext/hash/deep_merge'
20+
end
1021

1122
module Config
1223
extend Config::Validation::Schema

lib/config/options.rb

+26-18
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@ def reload!
4040
if conf.empty?
4141
conf = source_conf
4242
else
43-
DeepMerge.deep_merge!(
44-
source_conf,
45-
conf,
46-
preserve_unmergeables: false,
47-
knockout_prefix: Config.knockout_prefix,
48-
overwrite_arrays: Config.overwrite_arrays,
49-
merge_nil_values: Config.merge_nil_values,
50-
merge_hash_arrays: Config.merge_hash_arrays
51-
)
43+
if defined?(DeepMerge)
44+
DeepMerge.deep_merge!(
45+
source_conf,
46+
conf,
47+
preserve_unmergeables: false,
48+
knockout_prefix: Config.knockout_prefix,
49+
overwrite_arrays: Config.overwrite_arrays,
50+
merge_nil_values: Config.merge_nil_values,
51+
merge_hash_arrays: Config.merge_hash_arrays
52+
)
53+
else
54+
conf = conf.deep_merge!(source_conf)#source_conf.deep_merge!(conf)
55+
end
5256
end
5357
end
5458

@@ -98,15 +102,19 @@ def as_json(options = nil)
98102

99103
def merge!(hash)
100104
current = to_hash
101-
DeepMerge.deep_merge!(
102-
hash.dup,
103-
current,
104-
preserve_unmergeables: false,
105-
knockout_prefix: Config.knockout_prefix,
106-
overwrite_arrays: Config.overwrite_arrays,
107-
merge_nil_values: Config.merge_nil_values,
108-
merge_hash_arrays: Config.merge_hash_arrays
109-
)
105+
if defined?(DeepMerge)
106+
DeepMerge.deep_merge!(
107+
hash.dup,
108+
current,
109+
preserve_unmergeables: false,
110+
knockout_prefix: Config.knockout_prefix,
111+
overwrite_arrays: Config.overwrite_arrays,
112+
merge_nil_values: Config.merge_nil_values,
113+
merge_hash_arrays: Config.merge_hash_arrays
114+
)
115+
else
116+
current.deep_merge!(hash)
117+
end
110118
marshal_load(__convert(current).marshal_dump)
111119
self
112120
end

spec/config_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@
331331
end
332332

333333
it 'should not overwrite values with nil' do
334+
unless defined?(DeepMerge)
335+
skip <<~REASON
336+
DeepMerge is not available in the current context. This test only applies when the `deep_merge` gem is available.
337+
REASON
338+
end
334339
old_value = config.inner.something1
335340
config.merge!(hash_with_nil)
336341
expect(config.inner.something1).to eq(old_value)
@@ -418,6 +423,11 @@
418423
end
419424

420425
it 'should remove elements from settings' do
426+
unless defined?(DeepMerge)
427+
skip <<~REASON
428+
DeepMerge is not available in the current context. This test only applies when the `deep_merge` gem is available.
429+
REASON
430+
end
421431
expect(config.array1).to eq(['item4', 'item5', 'item6'])
422432
expect(config.array2.inner).to eq(['item4', 'item5', 'item6'])
423433
expect(config.array3).to eq('')
@@ -474,6 +484,11 @@
474484
end
475485

476486
it 'should merge arrays from multiple configs' do
487+
unless defined?(DeepMerge)
488+
skip <<~REASON
489+
DeepMerge is not available in the current context. This test only applies when the `deep_merge` gem is available.
490+
REASON
491+
end
477492
expect(config.arraylist1.size).to eq(6)
478493
expect(config.arraylist2.inner.size).to eq(6)
479494
end

spec/options_spec.rb

+35
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@
112112
end
113113

114114
it 'should overwrite the previous values' do
115+
unless defined?(DeepMerge)
116+
skip <<~REASON
117+
DeepMerge is not available in the current context. This test only applies when the `deep_merge` gem is available.
118+
REASON
119+
end
115120
expect(config['tvrage']['service_url']).to eq('http://url2')
116121
end
117122

@@ -124,6 +129,11 @@
124129
end
125130

126131
it 'should overwrite the previous values' do
132+
unless defined?(DeepMerge)
133+
skip <<~REASON
134+
DeepMerge is not available in the current context. This test only applies when the `deep_merge` gem is available.
135+
REASON
136+
end
127137
expect(config['tvrage']['service_url']).to eq('http://url3')
128138
end
129139
end
@@ -144,6 +154,11 @@
144154
end
145155

146156
it 'should add keys from the added file' do
157+
unless defined?(DeepMerge)
158+
skip <<~REASON
159+
DeepMerge is not available in the current context. This test only applies when the `deep_merge` gem is available.
160+
REASON
161+
end
147162
expect(config['tvrage']['service_url']).to eq('http://services.tvrage.com')
148163
end
149164

@@ -154,6 +169,11 @@
154169
end
155170

156171
it 'should overwrite the previous values' do
172+
unless defined?(DeepMerge)
173+
skip <<~REASON
174+
DeepMerge is not available in the current context. This test only applies when the `deep_merge` gem is available.
175+
REASON
176+
end
157177
expect(config['tvrage']['service_url']).to eq('http://services.tvrage.com')
158178
end
159179
end
@@ -168,6 +188,11 @@
168188
end
169189

170190
it 'should be overwritten by the following values' do
191+
unless defined?(DeepMerge)
192+
skip <<~REASON
193+
DeepMerge is not available in the current context. This test only applies when the `deep_merge` gem is available.
194+
REASON
195+
end
171196
expect(config['tvrage']['service_url']).to eq('http://services.tvrage.com')
172197
end
173198

@@ -246,6 +271,11 @@
246271
} }
247272

248273
it 'should merge the arrays' do
274+
unless defined?(DeepMerge)
275+
skip <<~REASON
276+
DeepMerge is not available in the current context. This test only applies when the `deep_merge` gem is available.
277+
REASON
278+
end
249279
config = Config.load_files("#{fixture_path}/deep_merge3/config1.yml", "#{fixture_path}/deep_merge3/config2.yml")
250280

251281
expect(config.array.length).to eq(1)
@@ -261,6 +291,11 @@
261291
} }
262292

263293
it 'should merge the arrays' do
294+
unless defined?(DeepMerge)
295+
skip <<~REASON
296+
DeepMerge is not available in the current context. This test only applies when the `deep_merge` gem is available.
297+
REASON
298+
end
264299
config = Config.load_files("#{fixture_path}/deep_merge3/config1.yml", "#{fixture_path}/deep_merge3/config2.yml")
265300

266301
expect(config.array.length).to eq(2)

0 commit comments

Comments
 (0)