Class: Configuration

Inherits:
Object
  • Object
show all
Includes:
File_Checking, Logging, Singleton, Translating
Defined in:
lib/configuration.rb

Constant Summary collapse

@@config_file =

default configuration file

File::dirname(__FILE__) << File::Separator << 'config'

Instance Method Summary collapse

Methods included from Translating

language, trl, #trl

Methods included from Logging

#init_logger, #log_level=, #log_target=

Methods included from File_Checking

#file_check, file_check

Constructor Details

#initializeConfiguration

do initializations



39
40
41
# File 'lib/configuration.rb', line 39

def initialize()
  init_logger()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *args) ⇒ Object

return any value stored in @config



69
70
71
72
73
74
75
76
77
78
# File 'lib/configuration.rb', line 69

def method_missing(msg, *args)
  ms = msg.to_sym
  # Exception-handling is not a control-structure.
  # This is.
  if @config[ms]
    return @config[ms]
  else
    return nil
  end
end

Instance Method Details

#set(options) ⇒ Object

Configure with the command-line arguments.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/configuration.rb', line 44

def set(options)
  @log.debug('options are: ' << options.to_s )
  # User-provided configuration-file?
  if(options['config'])
    cf = options['config'] 
    msg = check_file(cf, :file, :readable)
    if(!msg) 
      @@config_file = cf
    else
      msg = trl("The file %s " << msg.split[1,100].join(' ')) %msg.split[0]
      @log.error(trl("ERROR! Unsuitable file") << ' ' << msg)
      give_up
    end
  end

  @log.debug('config-file is ' << @@config_file)
  # read defaults from configuration-file
  co = OpenStruct.new(YAML::load_file(@@config_file))
  # merge and overwrite with the comman-line arguments
  @config = co.to_h.merge(options.to_h)
  @log.debug('config is now: ' << @config.to_s )
  verify
end