Module: Params
- Defined in:
- lib/params.rb,
lib/params/version.rb
Defined Under Namespace
Classes: Param
Constant Summary collapse
- VERSION =
"1.1.0"
Class Method Summary collapse
-
.[](name) ⇒ Object
Read param value by other modules.
-
.[]=(name, value) ⇒ Object
Write param value by other modules.
-
.boolean(name, value, description) ⇒ Object
Define new parameter of type Boolean.
- .complex(name, value, description) ⇒ Object
- .each(&block) ⇒ Object
-
.float(name, value, description) ⇒ Object
Define new parameter of type Float.
- .get_init_messages ⇒ Object
-
.init(args) ⇒ Object
Initializes the project parameters.
-
.integer(name, value, description) ⇒ Object
Define new parameter of type Integer.
-
.path(name, value, description) ⇒ Object
Define new parameter of type path (the only difference with string is that the path expends ‘~’ to full user directory).
-
.string(name, value, description) ⇒ Object
Define new parameter of type String.
-
.to_simple_hash ⇒ Object
end of Parse function.
Class Method Details
.[](name) ⇒ Object
Read param value by other modules. Note that this operator should only be used, after parameter has been defined through one of Param module methods: Params.string, Params.integer, Params.float or Params.boolean.“
168 169 170 171 |
# File 'lib/params.rb', line 168 def Params.[](name) raise_error_if_param_does_not_exist(name) @params_data_base[name].value end |
.[]=(name, value) ⇒ Object
Write param value by other modules. Note that this operator should only be used, after parameter has been defined through one of Param module methods: Params.string, Params.integer, Params.float or Params.boolean.“
181 182 183 184 185 |
# File 'lib/params.rb', line 181 def Params.[]=(name, value) raise_error_if_param_does_not_exist(name) set_value = @params_data_base[name].value_type_check(value) @params_data_base[name].value = set_value end |
.boolean(name, value, description) ⇒ Object
Define new parameter of type Boolean.
245 246 247 248 |
# File 'lib/params.rb', line 245 def Params.boolean(name, value, description) raise_error_if_param_exists(name) @params_data_base[name] = Param.new(name, value, 'Boolean', description) end |
.complex(name, value, description) ⇒ Object
239 240 241 242 |
# File 'lib/params.rb', line 239 def Params.complex(name, value, description) raise_error_if_param_exists(name) @params_data_base[name] = Param.new(name, value, 'Complex', description) end |
.each(&block) ⇒ Object
173 174 175 |
# File 'lib/params.rb', line 173 def Params.each(&block) @params_data_base.each(&block) end |
.float(name, value, description) ⇒ Object
Define new parameter of type Float.
220 221 222 223 |
# File 'lib/params.rb', line 220 def Params.float(name, value, description) raise_error_if_param_exists(name) @params_data_base[name] = Param.new(name, value, 'Float', description) end |
.get_init_messages ⇒ Object
65 66 67 |
# File 'lib/params.rb', line 65 def Params. return @init_debug_messages end |
.init(args) ⇒ Object
Initializes the project parameters. Precedence is: Defined params, file and command line is highest.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/params.rb', line 252 def Params.init(args) @init_debug_messages = [] results = parse_command_line_arguments(args) if not results['conf_file'].nil? Params['conf_file'] = File.(results['conf_file']) end #load yml params if path is provided and exists if Params['conf_file'].nil? @init_debug_messages << 'Configuration file was not provided.' + \ 'Skipping loading file parameters.' else if File.exist?(Params['conf_file']) @init_debug_messages << "Loading parameters from configuration file:'#{Params['conf_file']}'" if not read_yml_params(File.open(Params['conf_file'], 'r')) @init_debug_messages << "Bad configuration file #{Params['conf_file']}." end else @init_debug_messages << "Configuration file path:'#{Params['conf_file']}' does not exist. " + \ "Skipping loading file parameters." end end #override command line argument results.keys.each do |result_name| override_param(result_name, results[result_name]) end # Prints help and parameters if needed. if @show_help_and_exit # Print parameters + description and exit. puts "Full list of parameters:" Params.each do |name, param| puts "--#{name}, #{param.type}, default:#{param.value}\n\t#{param.desc}" end exit end # Add parameters to log init messages (used by Log.init if param:print_params_to_stdout is true) @init_debug_messages << 'Initialized executable parameters:' @init_debug_messages << '---------------------------------' counter=0 @params_data_base.values.each do |param| counter += 1 @init_debug_messages << "Param ##{counter}: #{param.name}=#{param.value}" end @init_debug_messages << '---------------------------------' end |
.integer(name, value, description) ⇒ Object
Define new parameter of type Integer.
214 215 216 217 |
# File 'lib/params.rb', line 214 def Params.integer(name, value, description) raise_error_if_param_exists(name) @params_data_base[name] = Param.new(name, value, 'Integer', description) end |
.path(name, value, description) ⇒ Object
Define new parameter of type path (the only difference with string is that the path expends ‘~’ to full user directory).
233 234 235 236 237 |
# File 'lib/params.rb', line 233 def Params.path(name, value, description) raise_error_if_param_exists(name) value = File.(value) unless value.nil? @params_data_base[name] = Param.new(name, value, 'Path', description) end |
.string(name, value, description) ⇒ Object
Define new parameter of type String.
226 227 228 229 |
# File 'lib/params.rb', line 226 def Params.string(name, value, description) raise_error_if_param_exists(name) @params_data_base[name] = Param.new(name, value, 'String', description) end |
.to_simple_hash ⇒ Object
end of Parse function
391 392 393 394 395 |
# File 'lib/params.rb', line 391 def Params.to_simple_hash @params_data_base.map { |param| param.value } end |