fedux_org-stdlib 0.9.4 → 0.9.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/fedux_org_stdlib/app_config.rb +42 -31
- data/lib/fedux_org_stdlib/version.rb +1 -1
- data/spec/app_config_spec.rb +60 -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: 95421cf63a2b9cfc2aadba02729ec31edb4f9c23
|
4
|
+
data.tar.gz: 88d4e3448de789088dd0091a665c362c4ee8215b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 152515d3efef6302211cedec9d75450aaca7ff5ed7dda382f309d526796a8c496b1f9003bd33c5cb151bd04d9b8713c7dd348724862a11d21a1d69547c2efafc
|
7
|
+
data.tar.gz: b252e98113cd114a7cf8a0a61e3e604a958e9eaeddb162970bfaacb47003a6a4a560b6a1821202d63339667ede7d6b308419144d875f0e5addc1af22832b0f27
|
data/Gemfile.lock
CHANGED
@@ -181,10 +181,11 @@ module FeduxOrgStdlib
|
|
181
181
|
# config_engine does not respond to `:[]` an empty config object will be
|
182
182
|
# created.
|
183
183
|
|
184
|
-
attr_reader :
|
184
|
+
attr_reader :files, :logger, :config_engine, :check_unknown_options, :working_directory, :safe, :merge_files
|
185
185
|
|
186
186
|
def initialize(
|
187
187
|
file: nil,
|
188
|
+
merge_files: false,
|
188
189
|
config_engine: Psych,
|
189
190
|
logger: FeduxOrgStdlib::Logging::Logger.new,
|
190
191
|
check_unknown_options: true,
|
@@ -192,13 +193,14 @@ module FeduxOrgStdlib
|
|
192
193
|
safe: true
|
193
194
|
)
|
194
195
|
@logger = logger
|
195
|
-
@
|
196
|
+
@files = Array(file)
|
197
|
+
@merge_files = merge_files
|
196
198
|
@config_engine = config_engine
|
197
199
|
@check_unknown_options = check_unknown_options
|
198
200
|
@working_directory = working_directory
|
199
201
|
@safe = safe
|
200
202
|
|
201
|
-
|
203
|
+
detect_files if @files.blank?
|
202
204
|
load_config
|
203
205
|
end
|
204
206
|
|
@@ -209,7 +211,7 @@ module FeduxOrgStdlib
|
|
209
211
|
|
210
212
|
# Redected configuration file and reload config afterwards
|
211
213
|
def redetect
|
212
|
-
|
214
|
+
detect_files
|
213
215
|
load_config
|
214
216
|
end
|
215
217
|
|
@@ -284,42 +286,51 @@ module FeduxOrgStdlib
|
|
284
286
|
|
285
287
|
private
|
286
288
|
|
287
|
-
def
|
288
|
-
@
|
289
|
+
def detect_files
|
290
|
+
@files = _available_config_file
|
289
291
|
end
|
290
292
|
|
291
293
|
def load_config
|
292
|
-
|
293
|
-
logger.debug "No configuration file found at #{_allowed_config_file_paths.to_list}, therefor I'm going to use an empty config object instead."
|
294
|
-
@__config = {}
|
294
|
+
@__config = {}
|
295
295
|
|
296
|
+
if files.blank?
|
297
|
+
logger.debug "No configuration files found at #{_allowed_config_file_paths.to_list}, therefor I'm going to use an empty config object instead."
|
296
298
|
return
|
297
299
|
end
|
298
300
|
|
299
|
-
begin
|
300
|
-
yaml = if safe
|
301
|
-
config_engine.safe_load(File.read(file), [Symbol, Regexp])
|
302
|
-
else
|
303
|
-
config_engine.load(File.read(file))
|
304
|
-
end
|
305
|
-
rescue StandardError => e
|
306
|
-
raise Exceptions::ConfigFileNotReadable, JSON.dump(message: e.message, file: file)
|
307
|
-
end
|
308
301
|
|
309
|
-
if
|
310
|
-
|
311
|
-
|
302
|
+
files = if merge_files
|
303
|
+
self.files
|
304
|
+
else
|
305
|
+
Array(self.files.first)
|
306
|
+
end
|
307
|
+
|
308
|
+
files.each do |f|
|
309
|
+
begin
|
310
|
+
yaml = if safe
|
311
|
+
config_engine.safe_load(File.read(f), [Symbol, Regexp])
|
312
|
+
else
|
313
|
+
config_engine.load(File.read(f))
|
314
|
+
end
|
315
|
+
rescue StandardError => e
|
316
|
+
raise Exceptions::ConfigFileNotReadable, JSON.dump(message: e.message, file: f)
|
317
|
+
end
|
318
|
+
|
319
|
+
next if yaml.blank?
|
320
|
+
|
321
|
+
unless yaml.is_a? Hash
|
322
|
+
logger.warn "There seems to be a problem transforming config file \"#{f}\" to a hash, therefor I will use an empty config object."
|
323
|
+
next
|
324
|
+
end
|
325
|
+
|
312
326
|
yaml = yaml.deep_symbolize_keys
|
313
327
|
|
314
328
|
yaml_withknown_options = yaml.deep_symbolize_keys.slice(*known_options)
|
315
329
|
unknown_options = yaml.keys - yaml_withknown_options.keys
|
316
330
|
|
317
|
-
logger.warn "Unknown config options #{(unknown_options).to_list} in config file #{
|
331
|
+
logger.warn "Unknown config options #{(unknown_options).to_list} in config file #{f} detected. Please define them in your config class or remove the entries in your config file or disable check via `check_unknown_options: false` to get rid of this warning." unless unknown_options.blank? && check_unknown_options == true
|
318
332
|
|
319
|
-
@__config
|
320
|
-
else
|
321
|
-
logger.warn "There seems to be a problem transforming config file \"#{file}\" to a hash, therefor I will use an empty config object."
|
322
|
-
@__config = {}
|
333
|
+
@__config.reverse_merge! Hash(yaml_withknown_options)
|
323
334
|
end
|
324
335
|
end
|
325
336
|
|
@@ -402,17 +413,17 @@ module FeduxOrgStdlib
|
|
402
413
|
end
|
403
414
|
|
404
415
|
def _available_config_file
|
405
|
-
_allowed_config_file_paths.
|
416
|
+
_allowed_config_file_paths.select { |f| ::File.exist? f }
|
406
417
|
end
|
407
418
|
|
408
419
|
def self._reserved_key_words
|
409
420
|
(methods | instance_methods | private_methods | private_instance_methods) - (Class.methods | Class.private_methods) | [:to_s]
|
410
421
|
end
|
411
422
|
|
412
|
-
def method_missing(*args, &block)
|
413
|
-
|
423
|
+
#def method_missing(*args, &block)
|
424
|
+
# $stderr.puts "Please check if you have defined an option for #{args.first}."
|
414
425
|
|
415
|
-
|
416
|
-
end
|
426
|
+
# super
|
427
|
+
#end
|
417
428
|
end
|
418
429
|
end
|
data/spec/app_config_spec.rb
CHANGED
@@ -86,6 +86,66 @@ RSpec.describe AppConfig do
|
|
86
86
|
expect(config.defaults.opt1).to eq 'foobar'
|
87
87
|
expect(config.opt1).to eq 'hello world'
|
88
88
|
end
|
89
|
+
|
90
|
+
it 'uses the first file found' do
|
91
|
+
config_klass = Class.new(AppConfig) do
|
92
|
+
option_reader :opt1, 'foobar'
|
93
|
+
option_reader :opt2, 'foobar'
|
94
|
+
|
95
|
+
def _class_name
|
96
|
+
'TestConfig'
|
97
|
+
end
|
98
|
+
|
99
|
+
def _module_name
|
100
|
+
'MyApplication'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
write_file '~/.config/my_application/tests.yaml', <<-EOS.strip_heredoc
|
105
|
+
---
|
106
|
+
opt1: hello world config
|
107
|
+
EOS
|
108
|
+
|
109
|
+
write_file '~/.tests.yaml', <<-EOS.strip_heredoc
|
110
|
+
---
|
111
|
+
opt1: hello world home
|
112
|
+
opt2: hello world home
|
113
|
+
EOS
|
114
|
+
|
115
|
+
config = config_klass.new(merge_files: false)
|
116
|
+
expect(config.opt1).to eq 'hello world config'
|
117
|
+
expect(config.opt2).to eq 'foobar'
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'merges files on request' do
|
121
|
+
config_klass = Class.new(AppConfig) do
|
122
|
+
option_reader :opt1, 'foobar'
|
123
|
+
option_reader :opt2, 'foobar'
|
124
|
+
|
125
|
+
def _class_name
|
126
|
+
'TestConfig'
|
127
|
+
end
|
128
|
+
|
129
|
+
def _module_name
|
130
|
+
'MyApplication'
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
write_file '~/.config/my_application/tests.yaml', <<-EOS.strip_heredoc
|
135
|
+
---
|
136
|
+
opt1: hello world config
|
137
|
+
EOS
|
138
|
+
|
139
|
+
write_file '~/.tests.yaml', <<-EOS.strip_heredoc
|
140
|
+
---
|
141
|
+
opt1: hello world home
|
142
|
+
opt2: hello world home
|
143
|
+
EOS
|
144
|
+
|
145
|
+
config = config_klass.new(merge_files: true)
|
146
|
+
expect(config.opt1).to eq 'hello world config'
|
147
|
+
expect(config.opt2).to eq 'hello world home'
|
148
|
+
end
|
89
149
|
end
|
90
150
|
|
91
151
|
context '#lock' do
|