I have already a solution for my problem, but I would be glad if some
kind
soul could have a look on it and give suggestion for improvement…
Problem: My application can be controled by a kind of “rc file”,
specifying
various options. The options are defined using the Ruby syntax for
defining
constants. For example:
Example rc file
TIMEOUT=130
LOGFILE=ENV[HOME]+"/tmp/appl.log"
There are many possible options, but of course not all of them need to
be specified.
My solution is basically to evaluate the option file and access the
constants. I am aware that this imposes a pretty high security risk,
since any Ruby code could be put into the rc file, but for my
application I can accept it - security is not a big issue in this
particular case.
An option is “set” by calling a certain function in my application.
Here is my code:
Evaluate RC file in the context of a module. The name of the
rc file is on variable rcfile.
eval(“module Rc\n”+File.read(rcfile)+"\nend")
Now set the options according to the RC file
Scheduler.setTimeout(Rc::TIMEOUT) if Rc.const_defined?(‘TIMEOUT’);
Logger.setLogFile(Rc::LOGFILE) if Rc.const_defined?(‘LOGFILE’);
… and so on
This works fine, but I don’t like it much. It is a lot of typing for
each option, and it is easy to make a typing error:
Ooops, typo (REMOTEHOST vs. REMOTE_HOST)
SshConnect.setHost(Rc::REMOTEHOST) if Rc.const_defined?(‘REMOTE_HOST’)
Any idea of how I could do it better?
Ronald