fedux_org-stdlib 0.6.7 → 0.6.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fedux_org_stdlib/app_config/exceptions.rb +3 -0
- data/lib/fedux_org_stdlib/app_config.rb +43 -33
- data/lib/fedux_org_stdlib/version.rb +1 -1
- data/spec/app_config_spec.rb +34 -16
- 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: d9e703de0319a8f92f9c1e72fc69158c014aa21f
|
4
|
+
data.tar.gz: a34707ffed29a092d49b31855d885ead61c8a3b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c571e1a5eb285d3645b1678955a07029c52d80ad108c510d4ba12f8d77e67d9cad7c83beae9779a9c1bc3acf6e420f9e6220d242bca9c6ff42325b7c17aaef0
|
7
|
+
data.tar.gz: 68d5bf20b8b276d4eb31e5aa4ae02cdd4fe27cd29adc72aa131dd60c21df397be728abf16f565766ddb34850ade47c75b8179b7b00c67fd5faaa429fda15e707
|
@@ -70,12 +70,12 @@ module FeduxOrgStdlib
|
|
70
70
|
class << self
|
71
71
|
|
72
72
|
# @api private
|
73
|
-
def
|
73
|
+
def _options
|
74
74
|
@options ||= Set.new
|
75
75
|
end
|
76
76
|
|
77
77
|
# @api private
|
78
|
-
def
|
78
|
+
def _process_environment
|
79
79
|
@process_environment ||= ProcessEnvironment.new
|
80
80
|
end
|
81
81
|
|
@@ -87,11 +87,15 @@ module FeduxOrgStdlib
|
|
87
87
|
# @param [Object] default_value
|
88
88
|
# The default value of this option
|
89
89
|
def option_reader(option, default_value)
|
90
|
-
|
91
|
-
|
90
|
+
option = option.to_sym
|
91
|
+
|
92
|
+
fail Exceptions::OptionNameForbidden, JSON.dump(option: option) if _reserved_key_words.include? option
|
93
|
+
|
94
|
+
define_method option do
|
95
|
+
_config.fetch(option, default_value)
|
92
96
|
end
|
93
97
|
|
94
|
-
self.
|
98
|
+
self._options << option
|
95
99
|
end
|
96
100
|
|
97
101
|
# Define a writer for option
|
@@ -108,15 +112,17 @@ module FeduxOrgStdlib
|
|
108
112
|
# @raise [Exceptions::ConfigLocked]
|
109
113
|
# If one tries to modified a locked config
|
110
114
|
def option_writer(option)
|
115
|
+
fail Exceptions::OptionNameForbidden, JSON.dump(option: option) if _reserved_key_words.include? "#{option}=".to_sym
|
116
|
+
|
111
117
|
define_method "#{option}=".to_sym do |value|
|
112
118
|
begin
|
113
|
-
|
119
|
+
_config[option.to_sym] = value
|
114
120
|
rescue RuntimeError
|
115
121
|
raise Exceptions::ConfigLocked
|
116
122
|
end
|
117
123
|
end
|
118
124
|
|
119
|
-
self.
|
125
|
+
self._options << option
|
120
126
|
end
|
121
127
|
|
122
128
|
# Define a writer and a reader for option
|
@@ -136,7 +142,7 @@ module FeduxOrgStdlib
|
|
136
142
|
|
137
143
|
# @!attribute [r] config
|
138
144
|
# Holds the config
|
139
|
-
attr_reader :
|
145
|
+
attr_reader :_config
|
140
146
|
|
141
147
|
public
|
142
148
|
|
@@ -163,15 +169,15 @@ module FeduxOrgStdlib
|
|
163
169
|
# config_engine does not respond to `:[]` an empty config object will be
|
164
170
|
# created.
|
165
171
|
def initialize(
|
166
|
-
file:
|
172
|
+
file: _available_config_file,
|
167
173
|
config_engine: Psych,
|
168
174
|
logger: FeduxOrgStdlib::Logging::Logger.new
|
169
175
|
)
|
170
176
|
@logger = logger
|
171
177
|
|
172
178
|
unless file
|
173
|
-
logger.debug "No configuration file found at #{
|
174
|
-
@
|
179
|
+
logger.debug "No configuration file found at #{_allowed_config_file_paths.to_list}, using an empty config."
|
180
|
+
@_config = HashWithIndifferentAccess.new
|
175
181
|
|
176
182
|
return
|
177
183
|
end
|
@@ -183,15 +189,15 @@ module FeduxOrgStdlib
|
|
183
189
|
end
|
184
190
|
|
185
191
|
if yaml.respond_to? :[]
|
186
|
-
@
|
192
|
+
@_config = HashWithIndifferentAccess.new(yaml.symbolize_keys)
|
187
193
|
else
|
188
|
-
@
|
194
|
+
@_config = HashWithIndifferentAccess.new
|
189
195
|
end
|
190
196
|
end
|
191
197
|
|
192
198
|
# Lock the configuration
|
193
199
|
def lock
|
194
|
-
|
200
|
+
_config.freeze
|
195
201
|
end
|
196
202
|
|
197
203
|
# Output a string presentation of the configuration
|
@@ -203,7 +209,7 @@ module FeduxOrgStdlib
|
|
203
209
|
result << sprintf("%20s | %s", 'option', 'value')
|
204
210
|
result << sprintf("%s + %s", '-' * 20, '-' * 80)
|
205
211
|
|
206
|
-
self.class.
|
212
|
+
self.class._options.each do |o|
|
207
213
|
result << sprintf("%20s | %s", o, Array(public_send(o)).join(', '))
|
208
214
|
end
|
209
215
|
|
@@ -218,15 +224,15 @@ module FeduxOrgStdlib
|
|
218
224
|
# The name of the config file. It defaults to `<config_name>.yaml`. If
|
219
225
|
# you want to use a different file name you need to overwrite this
|
220
226
|
# method.
|
221
|
-
def
|
222
|
-
"#{
|
227
|
+
def _config_file
|
228
|
+
"#{_config_name}#{_config_file_suffix}"
|
223
229
|
end
|
224
230
|
|
225
231
|
# The suffix of the config file
|
226
232
|
#
|
227
233
|
# @return [String]
|
228
234
|
# The suffix of the config file
|
229
|
-
def
|
235
|
+
def _config_file_suffix
|
230
236
|
'.yaml'
|
231
237
|
end
|
232
238
|
|
@@ -241,12 +247,12 @@ module FeduxOrgStdlib
|
|
241
247
|
# class ClientConfig; end
|
242
248
|
#
|
243
249
|
# This will result in `client` as base name for the config file.
|
244
|
-
def
|
245
|
-
unless (name =
|
250
|
+
def _config_name
|
251
|
+
unless (name = _class_name.sub(/Config/, '').underscore.pluralize).blank?
|
246
252
|
return name
|
247
253
|
end
|
248
254
|
|
249
|
-
fail Exceptions::ClassNameIsMissing, JSON.dump(klass:
|
255
|
+
fail Exceptions::ClassNameIsMissing, JSON.dump(klass: _class_name)
|
250
256
|
end
|
251
257
|
|
252
258
|
# The name of your application
|
@@ -262,8 +268,8 @@ module FeduxOrgStdlib
|
|
262
268
|
# This will be converted to
|
263
269
|
#
|
264
270
|
# my_application
|
265
|
-
def
|
266
|
-
|
271
|
+
def _application_name
|
272
|
+
_module_name.underscore
|
267
273
|
end
|
268
274
|
|
269
275
|
# The paths where to look for the config file
|
@@ -271,27 +277,31 @@ module FeduxOrgStdlib
|
|
271
277
|
# @return [Array]
|
272
278
|
# A list of paths where the config object should look for its config
|
273
279
|
# file.
|
274
|
-
def
|
280
|
+
def _allowed_config_file_paths
|
275
281
|
[
|
276
|
-
::File.expand_path(::File.join(self.class.
|
277
|
-
::File.expand_path(::File.join(self.class.
|
278
|
-
::File.expand_path(::File.join(self.class.
|
279
|
-
::File.expand_path(::File.join(self.class.
|
280
|
-
::File.expand_path(::File.join('/etc',
|
282
|
+
::File.expand_path(::File.join(self.class._process_environment.fetch('HOME'), '.config', _application_name, _config_file)),
|
283
|
+
::File.expand_path(::File.join(self.class._process_environment.fetch('HOME'), format('.%s', _application_name), _config_file)),
|
284
|
+
::File.expand_path(::File.join(self.class._process_environment.fetch('HOME'), format('.%s', _config_file))),
|
285
|
+
::File.expand_path(::File.join(self.class._process_environment.fetch('HOME'), format('.%src', _config_name))),
|
286
|
+
::File.expand_path(::File.join('/etc', _application_name, _config_file)),
|
281
287
|
# ::File.expand_path("../../../../files/#{config_file}", __FILE__),
|
282
288
|
]
|
283
289
|
end
|
284
290
|
|
285
|
-
def
|
291
|
+
def _class_name
|
286
292
|
self.class.name.to_s.demodulize
|
287
293
|
end
|
288
294
|
|
289
|
-
def
|
295
|
+
def _module_name
|
290
296
|
self.class.to_s.deconstantize
|
291
297
|
end
|
292
298
|
|
293
|
-
def
|
294
|
-
|
299
|
+
def _available_config_file
|
300
|
+
_allowed_config_file_paths.find { |f| ::File.exists? f }
|
301
|
+
end
|
302
|
+
|
303
|
+
def self._reserved_key_words
|
304
|
+
(methods | instance_methods | private_methods | private_instance_methods ) - (Class.methods | Class.private_methods ) | [:to_s]
|
295
305
|
end
|
296
306
|
end
|
297
307
|
end
|
data/spec/app_config_spec.rb
CHANGED
@@ -10,11 +10,11 @@ RSpec.describe AppConfig do
|
|
10
10
|
config_klass = Class.new(AppConfig) do
|
11
11
|
option :opt1, nil
|
12
12
|
|
13
|
-
def
|
13
|
+
def _class_name
|
14
14
|
'TestConfig'
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
17
|
+
def _module_name
|
18
18
|
'MyApplication'
|
19
19
|
end
|
20
20
|
end
|
@@ -32,11 +32,11 @@ RSpec.describe AppConfig do
|
|
32
32
|
config_klass = Class.new(AppConfig) do
|
33
33
|
option_reader :opt1, nil
|
34
34
|
|
35
|
-
def
|
35
|
+
def _class_name
|
36
36
|
'TestConfig'
|
37
37
|
end
|
38
38
|
|
39
|
-
def
|
39
|
+
def _module_name
|
40
40
|
'MyApplication'
|
41
41
|
end
|
42
42
|
end
|
@@ -50,6 +50,24 @@ RSpec.describe AppConfig do
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
+
it 'checks for reserved key words' do
|
54
|
+
with_environment 'HOME' => working_directory do
|
55
|
+
expect do
|
56
|
+
Class.new(AppConfig) do
|
57
|
+
option_reader :_config_file, nil
|
58
|
+
|
59
|
+
def _class_name
|
60
|
+
'TestConfig'
|
61
|
+
end
|
62
|
+
|
63
|
+
def _module_name
|
64
|
+
'MyApplication'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end.to raise_error FeduxOrgStdlib::AppConfig::Exceptions::OptionNameForbidden
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
53
71
|
end
|
54
72
|
|
55
73
|
context 'config files' do
|
@@ -63,11 +81,11 @@ RSpec.describe AppConfig do
|
|
63
81
|
config_klass = Class.new(AppConfig) do
|
64
82
|
option_reader :opt1, nil
|
65
83
|
|
66
|
-
def
|
84
|
+
def _class_name
|
67
85
|
'TestConfig'
|
68
86
|
end
|
69
87
|
|
70
|
-
def
|
88
|
+
def _module_name
|
71
89
|
'MyApplication'
|
72
90
|
end
|
73
91
|
end
|
@@ -87,11 +105,11 @@ RSpec.describe AppConfig do
|
|
87
105
|
config_klass = Class.new(AppConfig) do
|
88
106
|
option_reader :opt1, nil
|
89
107
|
|
90
|
-
def
|
108
|
+
def _class_name
|
91
109
|
'TestConfig'
|
92
110
|
end
|
93
111
|
|
94
|
-
def
|
112
|
+
def _module_name
|
95
113
|
'MyApplication'
|
96
114
|
end
|
97
115
|
end
|
@@ -111,11 +129,11 @@ RSpec.describe AppConfig do
|
|
111
129
|
config_klass = Class.new(AppConfig) do
|
112
130
|
option_reader :opt1, nil
|
113
131
|
|
114
|
-
def
|
132
|
+
def _class_name
|
115
133
|
'TestConfig'
|
116
134
|
end
|
117
135
|
|
118
|
-
def
|
136
|
+
def _module_name
|
119
137
|
'MyApplication'
|
120
138
|
end
|
121
139
|
end
|
@@ -135,11 +153,11 @@ RSpec.describe AppConfig do
|
|
135
153
|
config_klass = Class.new(AppConfig) do
|
136
154
|
option_reader :opt1, nil
|
137
155
|
|
138
|
-
def
|
156
|
+
def _class_name
|
139
157
|
'TestConfig'
|
140
158
|
end
|
141
159
|
|
142
|
-
def
|
160
|
+
def _module_name
|
143
161
|
'MyApplication'
|
144
162
|
end
|
145
163
|
end
|
@@ -159,11 +177,11 @@ RSpec.describe AppConfig do
|
|
159
177
|
config_klass = Class.new(AppConfig) do
|
160
178
|
option_reader :opt1, nil
|
161
179
|
|
162
|
-
def
|
180
|
+
def _class_name
|
163
181
|
'TestConfig'
|
164
182
|
end
|
165
183
|
|
166
|
-
def
|
184
|
+
def _module_name
|
167
185
|
'MyApplication'
|
168
186
|
end
|
169
187
|
end
|
@@ -180,11 +198,11 @@ RSpec.describe AppConfig do
|
|
180
198
|
option :opt1, 'test1'
|
181
199
|
option :opt2, 'test2'
|
182
200
|
|
183
|
-
def
|
201
|
+
def _class_name
|
184
202
|
'TestConfig'
|
185
203
|
end
|
186
204
|
|
187
|
-
def
|
205
|
+
def _module_name
|
188
206
|
'MyApplication'
|
189
207
|
end
|
190
208
|
end
|