Class: Configuration
- Inherits:
-
Object
- Object
- Configuration
- Includes:
- Logging, 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
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.
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 Logging
#init_logger, #log_level=, #log_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 |
# File 'lib/configuration.rb', line 30 def 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. if !File.exist?(@config_file) begin File.open(@config_file, 'w') do |cf| cf.write(File.read( File::dirname(File::absolute_path(__FILE__)) << File::Separator << confname) ) end rescue => ex STDERR.puts('Cannot write user-configuration to ' << @config_file << '! (' << ex. << ')' ) end end # read the config-file. read_config # initialize the object-level logger according to configuration. @log = init_logger(log_target, log_level) 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.
51 52 53 54 55 |
# File 'lib/configuration.rb', line 51 def method_missing(method, args = nil) v = @conf[method] @log.debug('method_missing returns value for ' << method.to_s << ': |' << v.to_s << '|') return v end |
Instance Attribute Details
#log_level ⇒ Object (readonly)
Returns the value of attribute log_level.
62 63 64 |
# File 'lib/configuration.rb', line 62 def log_level @log_level end |
#log_target ⇒ Object (readonly)
Returns the value of attribute log_target.
62 63 64 |
# File 'lib/configuration.rb', line 62 def log_target @log_target end |
Instance Method Details
#set(key, value) ⇒ Object
set a configuration option.
58 59 60 |
# File 'lib/configuration.rb', line 58 def set(key, value) @conf[key] = value end |