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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76ef74f7ac7384488eb75228e902abeb2e21dfee
4
- data.tar.gz: ff9585596de37c99b38737c540c254c44ef4bc78
3
+ metadata.gz: 95421cf63a2b9cfc2aadba02729ec31edb4f9c23
4
+ data.tar.gz: 88d4e3448de789088dd0091a665c362c4ee8215b
5
5
  SHA512:
6
- metadata.gz: def728359046ad26adc4eacccd56600845d01ad4b1563297192506a063be05f2f1787d99ea1606b67fdb08ab1fe14704dfd01c53c765ca2dc031b06215cec838
7
- data.tar.gz: 4adf3fb8af2604cc0dd2f296447e4f665eab7bb960430d0c7d57da3e0adbf3e4bf3064e95a1a4944a41e49b9b6b9be4d3800c205ecea073087fb70968f1bd150
6
+ metadata.gz: 152515d3efef6302211cedec9d75450aaca7ff5ed7dda382f309d526796a8c496b1f9003bd33c5cb151bd04d9b8713c7dd348724862a11d21a1d69547c2efafc
7
+ data.tar.gz: b252e98113cd114a7cf8a0a61e3e604a958e9eaeddb162970bfaacb47003a6a4a560b6a1821202d63339667ede7d6b308419144d875f0e5addc1af22832b0f27
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fedux_org-stdlib (0.9.3)
4
+ fedux_org-stdlib (0.9.4)
5
5
  activesupport
6
6
 
7
7
  PATH
@@ -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 :file, :logger, :config_engine, :check_unknown_options, :working_directory, :safe
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
- @file = file
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
- detect_file unless @file
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
- detect_file
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 detect_file
288
- @file = _available_config_file
289
+ def detect_files
290
+ @files = _available_config_file
289
291
  end
290
292
 
291
293
  def load_config
292
- unless file
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 yaml.blank?
310
- @__config = {}
311
- elsif yaml.is_a? Hash
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 #{file} 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
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 = Hash(yaml_withknown_options)
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.find { |f| ::File.exist? f }
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
- $stderr.puts "Please check if you have defined an option for #{args.first}."
423
+ #def method_missing(*args, &block)
424
+ # $stderr.puts "Please check if you have defined an option for #{args.first}."
414
425
 
415
- super
416
- end
426
+ # super
427
+ #end
417
428
  end
418
429
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
  # FeduxOrgStdlib
3
3
  module FeduxOrgStdlib
4
- VERSION = '0.9.4'
4
+ VERSION = '0.9.5'
5
5
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fedux_org-stdlib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Meyer