fedux_org-stdlib 0.6.11 → 0.6.12

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: aa0d7a7ab30a474b38ae886c1762c787216540ba
4
- data.tar.gz: a28c29b1299b5f4e2b9db259381b785bcc9a32c3
3
+ metadata.gz: 1b6d4b263098c38be3d3499fdd03734a2c1efbd8
4
+ data.tar.gz: c7a34b1c7f3b72378eea3c10bdfe9b6d76723cfd
5
5
  SHA512:
6
- metadata.gz: 1085c790d1c8453489989abdd8cc7914e7431b98a015ee0d16bcffabea8f805d7c4bf9d60ca17d0e66b9b6481ed095c1e18a8f755ce2cae11f48bd0695f69e43
7
- data.tar.gz: 3ddb778fa2e54966f92c9c87cb60b99e3edf4a1ed300f3f9019bb7104853ea8d769779cce3ed0fdbd8c98f8259bdf03073bbcdb296adbdc73ff1e87adb1b3898
6
+ metadata.gz: cb1230859eae20347dfd17ef155d45026b8271a6dc9ebc9e2d6515e4e5ab188e916b2ff2190e382fe7f502a6ab777b2bccf1b41408ea0cc459659be7d6f89bc8
7
+ data.tar.gz: 80459da9eb8de1d77e37ef78cad19d8f0aa2ace3e405b827ee342fa4f2b136e51291712ae46112ef60663aef5c09b2e4cc6325630d762fa4f51efc1772107fee
@@ -4,7 +4,7 @@ require 'fedux_org_stdlib/app_config/exceptions'
4
4
  require 'fedux_org_stdlib/process_environment'
5
5
  require 'fedux_org_stdlib/core_ext/array'
6
6
  require 'fedux_org_stdlib/logging/logger'
7
- require_library %w{ json psych active_support/core_ext/string/inflections thread set active_support/hash_with_indifferent_access }
7
+ require_library %w{ json psych active_support/core_ext/string/inflections set active_support/core_ext/hash/slice }
8
8
 
9
9
  module FeduxOrgStdlib
10
10
  # This class makes a config file available as an object. The config file
@@ -71,7 +71,7 @@ module FeduxOrgStdlib
71
71
  class << self
72
72
 
73
73
  # @api private
74
- def _options
74
+ def _known_options
75
75
  @options ||= Set.new
76
76
  end
77
77
 
@@ -103,7 +103,7 @@ module FeduxOrgStdlib
103
103
  _config.fetch(option, default_value)
104
104
  end
105
105
 
106
- self._options << option
106
+ self._known_options << option
107
107
  end
108
108
 
109
109
  # Define a writer for option
@@ -130,7 +130,7 @@ module FeduxOrgStdlib
130
130
  end
131
131
  end
132
132
 
133
- self._options << option
133
+ self._known_options << option
134
134
  end
135
135
 
136
136
  # Define a writer and a reader for option
@@ -148,9 +148,10 @@ module FeduxOrgStdlib
148
148
 
149
149
  private
150
150
 
151
- # @!attribute [r] config
152
- # Holds the config
153
- attr_reader :_config
151
+ # Holds the config
152
+ def _config
153
+ @__config
154
+ end
154
155
 
155
156
  public
156
157
 
@@ -166,6 +167,10 @@ module FeduxOrgStdlib
166
167
  # @param [Object] config_engine (Psych)
167
168
  # The engine to read config file
168
169
  #
170
+ # @param [true, false] check_unknown_options
171
+ # Should a warning be put to stdout if there are unknown options in yaml
172
+ # config file
173
+ #
169
174
  # @raise [Exceptions::ConfigFileNotReadable]
170
175
  # If an avaiable config file could not be read by the config engine
171
176
  #
@@ -176,27 +181,36 @@ module FeduxOrgStdlib
176
181
  def initialize(
177
182
  file: _available_config_file,
178
183
  config_engine: Psych,
179
- logger: FeduxOrgStdlib::Logging::Logger.new
184
+ logger: FeduxOrgStdlib::Logging::Logger.new,
185
+ check_unknown_options: true
180
186
  )
181
187
  @logger = logger
182
188
 
183
189
  unless file
184
190
  logger.debug "No configuration file found at #{_allowed_config_file_paths.to_list}, using an empty config."
185
- @_config = HashWithIndifferentAccess.new
191
+ @__config = {}
186
192
 
187
193
  return
188
194
  end
189
195
 
190
196
  begin
191
- yaml = Psych.safe_load(File.read(file))
197
+ yaml = Psych.safe_load(File.read(file), [Symbol])
192
198
  rescue StandardError => e
193
199
  fail Exceptions::ConfigFileNotReadable, JSON.dump(message: e.message, file: file)
194
200
  end
195
201
 
196
- if yaml.respond_to? :[]
197
- @_config = HashWithIndifferentAccess.new(yaml.symbolize_keys)
202
+ if yaml.respond_to? :[] and !yaml.kind_of? Array
203
+ yaml = yaml.deep_symbolize_keys
204
+
205
+ yaml_with_known_options = yaml.deep_symbolize_keys.slice(*self.class._known_options)
206
+ unknown_options = yaml.keys - yaml_with_known_options.keys
207
+
208
+ 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
209
+
210
+ @__config = Hash(yaml_with_known_options)
198
211
  else
199
- @_config = HashWithIndifferentAccess.new
212
+ logger.warn "There seems to be a problem transforming config file \"#{file}\" to a hash using an empty config."
213
+ @__config = {}
200
214
  end
201
215
  end
202
216
 
@@ -214,7 +228,7 @@ module FeduxOrgStdlib
214
228
  result << sprintf("%20s | %s", 'option', 'value')
215
229
  result << sprintf("%s + %s", '-' * 20, '-' * 80)
216
230
 
217
- self.class._options.each do |o|
231
+ self.class._known_options.each do |o|
218
232
  result << sprintf("%20s | %s", o, Array(public_send(o)).join(', '))
219
233
  end
220
234
 
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
  # FeduxOrgStdlib
3
3
  module FeduxOrgStdlib
4
- VERSION = '0.6.11'
4
+ VERSION = '0.6.12'
5
5
  end
@@ -67,7 +67,28 @@ RSpec.describe AppConfig do
67
67
  end.to raise_error FeduxOrgStdlib::AppConfig::Exceptions::OptionNameForbidden
68
68
  end
69
69
  end
70
+ end
71
+
72
+ context '#lock' do
73
+ it 'raises error if config is locked and one tries to modify a config option' do
74
+ with_environment 'HOME' => working_directory do
75
+ config_klass = Class.new(AppConfig) do
76
+ option :opt1, nil
70
77
 
78
+ def _class_name
79
+ 'TestConfig'
80
+ end
81
+
82
+ def _module_name
83
+ 'MyApplication'
84
+ end
85
+ end
86
+
87
+ config = config_klass.new
88
+ config.lock
89
+ expect { config.opt1 = 1 }.to raise_error FeduxOrgStdlib::AppConfig::Exceptions::ConfigLocked
90
+ end
91
+ end
71
92
  end
72
93
 
73
94
  context 'config files' do
@@ -118,76 +139,160 @@ RSpec.describe AppConfig do
118
139
  expect(config.opt1).to eq 'hello world'
119
140
  end
120
141
  end
121
- end
122
- it 'looks at ~/.my_application/tests.yaml' do
123
- with_environment 'HOME' => working_directory do
124
- create_file '.my_application/tests.yaml', <<-EOS.strip_heredoc
142
+
143
+ it 'looks at ~/.my_application/tests.yaml' do
144
+ with_environment 'HOME' => working_directory do
145
+ create_file '.my_application/tests.yaml', <<-EOS.strip_heredoc
125
146
  ---
126
147
  opt1: hello world
127
- EOS
148
+ EOS
128
149
 
129
- config_klass = Class.new(AppConfig) do
130
- option_reader :opt1, nil
150
+ config_klass = Class.new(AppConfig) do
151
+ option_reader :opt1, nil
131
152
 
132
- def _class_name
133
- 'TestConfig'
134
- end
153
+ def _class_name
154
+ 'TestConfig'
155
+ end
135
156
 
136
- def _module_name
137
- 'MyApplication'
157
+ def _module_name
158
+ 'MyApplication'
159
+ end
138
160
  end
139
- end
140
161
 
141
- config = config_klass.new
142
- expect(config.opt1).to eq 'hello world'
162
+ config = config_klass.new
163
+ expect(config.opt1).to eq 'hello world'
164
+ end
143
165
  end
144
- end
145
166
 
146
- it 'looks at ~/.tests.yaml' do
147
- with_environment 'HOME' => working_directory do
148
- create_file '.tests.yaml', <<-EOS.strip_heredoc
167
+ it 'looks at ~/.tests.yaml' do
168
+ with_environment 'HOME' => working_directory do
169
+ create_file '.tests.yaml', <<-EOS.strip_heredoc
149
170
  ---
150
171
  opt1: hello world
151
- EOS
172
+ EOS
152
173
 
153
- config_klass = Class.new(AppConfig) do
154
- option_reader :opt1, nil
174
+ config_klass = Class.new(AppConfig) do
175
+ option_reader :opt1, nil
155
176
 
156
- def _class_name
157
- 'TestConfig'
158
- end
177
+ def _class_name
178
+ 'TestConfig'
179
+ end
159
180
 
160
- def _module_name
161
- 'MyApplication'
181
+ def _module_name
182
+ 'MyApplication'
183
+ end
162
184
  end
185
+
186
+ config = config_klass.new
187
+ expect(config.opt1).to eq 'hello world'
163
188
  end
189
+ end
190
+
191
+ it 'looks at ~/.testsrc' do
192
+ with_environment 'HOME' => working_directory do
193
+ create_file '.testsrc', <<-EOS.strip_heredoc
194
+ ---
195
+ opt1: hello world
196
+ EOS
197
+
198
+ config_klass = Class.new(AppConfig) do
199
+ option_reader :opt1, nil
164
200
 
165
- config = config_klass.new
166
- expect(config.opt1).to eq 'hello world'
201
+ def _class_name
202
+ 'TestConfig'
203
+ end
204
+
205
+ def _module_name
206
+ 'MyApplication'
207
+ end
208
+ end
209
+
210
+ config = config_klass.new
211
+ expect(config.opt1).to eq 'hello world'
212
+ end
167
213
  end
168
- end
169
214
 
170
- it 'looks at ~/.testsrc' do
171
- with_environment 'HOME' => working_directory do
172
- create_file '.testsrc', <<-EOS.strip_heredoc
215
+ it 'loads yaml files with the following data types' do
216
+ with_environment 'HOME' => working_directory do
217
+ create_file '.testsrc', <<-EOS.strip_heredoc
173
218
  ---
174
219
  opt1: hello world
175
- EOS
220
+ opt2: :test
221
+ opt3: 1
222
+ opt4: true
223
+ opt5: false
224
+ opt6:
225
+ - a
226
+ - b
227
+ opt7:
228
+ name: berni
229
+ EOS
176
230
 
177
- config_klass = Class.new(AppConfig) do
178
- option_reader :opt1, nil
231
+ config_klass = Class.new(AppConfig) do
232
+ option_reader :opt1, nil
233
+ option_reader :opt2, nil
234
+ option_reader :opt3, nil
235
+ option_reader :opt4, nil
236
+ option_reader :opt5, nil
237
+ option_reader :opt6, nil
238
+ option_reader :opt7, nil
179
239
 
180
- def _class_name
181
- 'TestConfig'
240
+ def _class_name
241
+ 'TestConfig'
242
+ end
243
+
244
+ def _module_name
245
+ 'MyApplication'
246
+ end
182
247
  end
183
248
 
184
- def _module_name
185
- 'MyApplication'
249
+ config = config_klass.new
250
+ expect(config.opt1).to eq 'hello world'
251
+ end
252
+ end
253
+
254
+ it 'raises error on invalid yaml file' do
255
+ with_environment 'HOME' => working_directory do
256
+ create_file '.testsrc', random_binary_string(100)
257
+
258
+ config_klass = Class.new(AppConfig) do
259
+ def _class_name
260
+ 'TestConfig'
261
+ end
262
+
263
+ def _module_name
264
+ 'MyApplication'
265
+ end
186
266
  end
267
+
268
+ expect { config_klass.new }.to raise_error FeduxOrgStdlib::AppConfig::Exceptions::ConfigFileNotReadable
187
269
  end
270
+ end
188
271
 
189
- config = config_klass.new
190
- expect(config.opt1).to eq 'hello world'
272
+ it 'returns an empty config if data file cannot be transformed to hash' do
273
+ with_environment 'HOME' => working_directory do
274
+ create_file '.testsrc', <<-EOS.strip_heredoc
275
+ ---
276
+ - hello
277
+ - world
278
+ EOS
279
+
280
+ config_klass = Class.new(AppConfig) do
281
+ def _class_name
282
+ 'TestConfig'
283
+ end
284
+
285
+ def _module_name
286
+ 'MyApplication'
287
+ end
288
+ end
289
+
290
+ result = capture :stderr do
291
+ config_klass.new
292
+ end
293
+
294
+ expect(result).to include "There seems"
295
+ end
191
296
  end
192
297
  end
193
298
 
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ module FeduxOrgStdlib
3
+ module SpecHelper
4
+ module Generators
5
+ def random_binary_string(length)
6
+ Random.new.bytes(length)
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ RSpec.configure do |c|
13
+ c.include FeduxOrgStdlib::SpecHelper::Generators
14
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fedux_org-stdlib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.11
4
+ version: 0.6.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Meyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-15 00:00:00.000000000 Z
11
+ date: 2014-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -159,6 +159,7 @@ files:
159
159
  - spec/spec_helper.rb
160
160
  - spec/support/environment.rb
161
161
  - spec/support/filesystem.rb
162
+ - spec/support/generator.rb
162
163
  - spec/support/reporting.rb
163
164
  - spec/support/rspec.rb
164
165
  - spec/support/string.rb
@@ -224,6 +225,7 @@ test_files:
224
225
  - spec/spec_helper.rb
225
226
  - spec/support/environment.rb
226
227
  - spec/support/filesystem.rb
228
+ - spec/support/generator.rb
227
229
  - spec/support/reporting.rb
228
230
  - spec/support/rspec.rb
229
231
  - spec/support/string.rb