Class: Configuration
- Inherits:
-
Object
- Object
- Configuration
- Extended by:
- BasicLogging
- Includes:
- BasicLogging, Singleton
- Defined in:
- lib/configuration.rb
Overview
An object of this class represents the configuration of the program. The parameters are set in the configuration-file
Constant Summary
Constants included from BasicLogging
BasicLogging::DEBUG, BasicLogging::ERROR, BasicLogging::FATAL, BasicLogging::INFO, BasicLogging::Levels, BasicLogging::UNKNOWN, BasicLogging::WARN
Instance Attribute Summary collapse
-
#log_level ⇒ Object
readonly
Returns the value of attribute log_level.
-
#log_target ⇒ Object
readonly
Returns the value of attribute log_target.
Attributes included from BasicLogging
Instance Method Summary collapse
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#method_missing(method, args = nil) ⇒ Object
What this object does not have, may still be in the configuration.
-
#set(key, value) ⇒ Object
set a configuration option.
Methods included from BasicLogging
is_muted?, log, mute, set_level, set_target
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/configuration.rb', line 30 def initialize() debug 'Configuration::initialize()' confname = PROGNAME << '.conf' # try to open user-configuration @config_file = ENV['HOME'].dup << File::Separator << '.' << confname # if user-configuration does not exist, copy installed version. installed_config = File::dirname(File::absolute_path(__FILE__)) << File::Separator << confname debug 'installed configuration is in ' << installed_config if !File.exist?(@config_file) begin File.open(@config_file, 'w') do |cf| cf.write(File.read(installed_config ) ) end rescue => ex STDERR.puts('Cannot write user-configuration to ' << @config_file << '! (' << ex. << ')' ) end end # read the config-file. read_config # updates the user-configuration *ONLY* # if necessary update_config(installed_config) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, args = nil) ⇒ Object
What this object does not have, may still be in the configuration.
55 56 57 58 59 60 61 62 63 |
# File 'lib/configuration.rb', line 55 def method_missing(method, args = nil) if @conf v = @conf[method] debug('method_missing returns value for ' << method.to_s << ': |' << v.to_s << '|') return v else error("user-version of the configuration (#{@conf}) is not accessible") end end |
Instance Attribute Details
#log_level ⇒ Object (readonly)
Returns the value of attribute log_level.
75 76 77 |
# File 'lib/configuration.rb', line 75 def log_level @log_level end |
#log_target ⇒ Object (readonly)
Returns the value of attribute log_target.
75 76 77 |
# File 'lib/configuration.rb', line 75 def log_target @log_target end |
Instance Method Details
#set(key, value) ⇒ Object
set a configuration option.
66 67 68 69 70 71 72 73 |
# File 'lib/configuration.rb', line 66 def set(key, value) if @conf @conf[key] = value if key == 'DEBUG_LOG' set_target value end end end |