params 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/params.rb +27 -33
- data/lib/params/version.rb +1 -1
- data/test/params/params_spec.rb +1 -1
- metadata +2 -2
data/lib/params.rb
CHANGED
@@ -122,7 +122,6 @@ module Params
|
|
122
122
|
end
|
123
123
|
end
|
124
124
|
when 'Complex' then
|
125
|
-
when 'Global' then
|
126
125
|
unless @value.nil?
|
127
126
|
unless (value.class.eql? Hash) or (value.class.eql? Array)
|
128
127
|
raise("Parameter:'#{@name}' type:'Complex' but value type to override " \
|
@@ -146,52 +145,52 @@ module Params
|
|
146
145
|
end
|
147
146
|
end
|
148
147
|
|
149
|
-
# The
|
150
|
-
@
|
148
|
+
# The parameters data structure.
|
149
|
+
@params_data_base = Hash.new
|
151
150
|
|
152
151
|
def Params.raise_error_if_param_does_not_exist(name)
|
153
|
-
if not @
|
152
|
+
if not @params_data_base[name]
|
154
153
|
raise("before using parameter:'#{name}', it should first be defined through Param module methods:" \
|
155
|
-
"Params.string, Params.path, Params.integer, Params.float or Params.boolean.")
|
154
|
+
"Params.string, Params.path, Params.integer, Params.float, Params.complex, or Params.boolean.")
|
156
155
|
end
|
157
156
|
end
|
158
157
|
|
159
158
|
def Params.raise_error_if_param_exists(name)
|
160
|
-
if @
|
159
|
+
if @params_data_base[name]
|
161
160
|
raise("Parameter:'#{name}', can only be defined once.")
|
162
161
|
end
|
163
162
|
end
|
164
163
|
|
165
|
-
# Read
|
164
|
+
# Read param value by other modules.
|
166
165
|
# Note that this operator should only be used, after parameter has been defined through
|
167
166
|
# one of Param module methods: Params.string, Params.integer,
|
168
167
|
# Params.float or Params.boolean."
|
169
168
|
def Params.[](name)
|
170
169
|
raise_error_if_param_does_not_exist(name)
|
171
|
-
@
|
170
|
+
@params_data_base[name].value
|
172
171
|
end
|
173
172
|
|
174
173
|
def Params.each(&block)
|
175
|
-
@
|
174
|
+
@params_data_base.each(&block)
|
176
175
|
end
|
177
176
|
|
178
|
-
# Write
|
177
|
+
# Write param value by other modules.
|
179
178
|
# Note that this operator should only be used, after parameter has been defined through
|
180
179
|
# one of Param module methods: Params.string, Params.integer,
|
181
180
|
# Params.float or Params.boolean."
|
182
181
|
def Params.[]=(name, value)
|
183
182
|
raise_error_if_param_does_not_exist(name)
|
184
|
-
set_value = @
|
185
|
-
@
|
183
|
+
set_value = @params_data_base[name].value_type_check(value)
|
184
|
+
@params_data_base[name].value = set_value
|
186
185
|
end
|
187
186
|
|
188
187
|
#override parameter should only be called by Params module methods.
|
189
188
|
def Params.override_param(name, value)
|
190
|
-
existing_param = @
|
189
|
+
existing_param = @params_data_base[name]
|
191
190
|
if existing_param.nil?
|
192
191
|
raise("Parameter:'#{name}' has not been defined and can not be overridden. " \
|
193
192
|
"It should first be defined through Param module methods:" \
|
194
|
-
"Params.string, Params.path, Params.integer, Params.float or Params.boolean.")
|
193
|
+
"Params.string, Params.path, Params.integer, Params.float, Params.complex, or Params.boolean.")
|
195
194
|
end
|
196
195
|
if value.nil?
|
197
196
|
existing_param.value = nil
|
@@ -211,46 +210,41 @@ module Params
|
|
211
210
|
end
|
212
211
|
end
|
213
212
|
|
214
|
-
# Define new
|
213
|
+
# Define new parameter of type Integer.
|
215
214
|
def Params.integer(name, value, description)
|
216
215
|
raise_error_if_param_exists(name)
|
217
|
-
@
|
216
|
+
@params_data_base[name] = Param.new(name, value, 'Integer', description)
|
218
217
|
end
|
219
218
|
|
220
|
-
# Define new
|
219
|
+
# Define new parameter of type Float.
|
221
220
|
def Params.float(name, value, description)
|
222
221
|
raise_error_if_param_exists(name)
|
223
|
-
@
|
222
|
+
@params_data_base[name] = Param.new(name, value, 'Float', description)
|
224
223
|
end
|
225
224
|
|
226
|
-
# Define new
|
225
|
+
# Define new parameter of type String.
|
227
226
|
def Params.string(name, value, description)
|
228
227
|
raise_error_if_param_exists(name)
|
229
|
-
@
|
228
|
+
@params_data_base[name] = Param.new(name, value, 'String', description)
|
230
229
|
end
|
231
230
|
|
232
|
-
# Define new
|
231
|
+
# Define new parameter of type path (the only difference with string is that the path expends '~' to
|
233
232
|
# full user directory).
|
234
233
|
def Params.path(name, value, description)
|
235
234
|
raise_error_if_param_exists(name)
|
236
235
|
value = File.expand_path(value) unless value.nil?
|
237
|
-
@
|
236
|
+
@params_data_base[name] = Param.new(name, value, 'Path', description)
|
238
237
|
end
|
239
238
|
|
240
239
|
def Params.complex(name, value, description)
|
241
240
|
raise_error_if_param_exists(name)
|
242
|
-
@
|
241
|
+
@params_data_base[name] = Param.new(name, value, 'Complex', description)
|
243
242
|
end
|
244
243
|
|
245
|
-
|
246
|
-
raise_error_if_param_exists(name)
|
247
|
-
@globals_db[name] = Param.new(name, value, 'Global', description)
|
248
|
-
end
|
249
|
-
|
250
|
-
# Define new global parameter of type Boolean.
|
244
|
+
# Define new parameter of type Boolean.
|
251
245
|
def Params.boolean(name, value, description)
|
252
246
|
raise_error_if_param_exists(name)
|
253
|
-
@
|
247
|
+
@params_data_base[name] = Param.new(name, value, 'Boolean', description)
|
254
248
|
end
|
255
249
|
|
256
250
|
# Initializes the project parameters.
|
@@ -297,7 +291,7 @@ module Params
|
|
297
291
|
@init_debug_messages << 'Initialized executable parameters:'
|
298
292
|
@init_debug_messages << '---------------------------------'
|
299
293
|
counter=0
|
300
|
-
@
|
294
|
+
@params_data_base.values.each do |param|
|
301
295
|
counter += 1
|
302
296
|
@init_debug_messages << "Param ##{counter}: #{param.name}=#{param.value}"
|
303
297
|
end
|
@@ -324,7 +318,7 @@ module Params
|
|
324
318
|
# Define List of options see example on
|
325
319
|
# http://ruby.about.com/od/advancedruby/a/optionparser2.htm
|
326
320
|
opts = OptionParser.new do |opts|
|
327
|
-
@
|
321
|
+
@params_data_base.values.each do |param|
|
328
322
|
tmp_name_long = "--#{param.name} #{param.name.upcase}" # Define a command with single mandatory parameter
|
329
323
|
tmp_value = param.desc + " Default value:" + param.value.to_s # Description and Default value
|
330
324
|
|
@@ -395,7 +389,7 @@ paths:
|
|
395
389
|
end # end of Parse function
|
396
390
|
|
397
391
|
def Params.to_simple_hash
|
398
|
-
@
|
392
|
+
@params_data_base.map { |param|
|
399
393
|
param.value
|
400
394
|
}
|
401
395
|
end
|
data/lib/params/version.rb
CHANGED
data/test/params/params_spec.rb
CHANGED
@@ -45,7 +45,7 @@ module Params
|
|
45
45
|
expect { Params::read_yml_params StringIO.new 'not_defined: 10' }.to raise_error \
|
46
46
|
"Parameter:'not_defined' has not been defined and can not be overridden. " \
|
47
47
|
"It should first be defined through Param module methods:" \
|
48
|
-
"Params.string, Params.path, Params.integer, Params.float or Params.boolean."
|
48
|
+
"Params.string, Params.path, Params.integer, Params.float, Params.complex, or Params.boolean."
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'Will test yml parameter loading' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-06-
|
13
|
+
date: 2013-06-24 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: Dynamically stores, parses and providers params. Uses module local readers.
|
16
16
|
email: bbfsdev@gmail.com
|