Class: Configuration
- Inherits:
-
Object
- Object
- Configuration
- Includes:
- BasicLogging, File_Checking, Singleton
- Defined in:
- lib/configuration.rb
Overview
An object of the Configuration class reads and exposes options from a configuration-file in YAML format.
Constant Summary collapse
- @@RD =
File::(File::dirname(__FILE__) ) + File::Separator
- @@config_file =
"#{@@RD}config"
- @@USER_CONF_DIR =
File::join(ENV['HOME'].dup, '.cremefraiche' )
- @@USER_CONF =
File::join(@@USER_CONF_DIR.dup, 'config')
Constants included from BasicLogging
BasicLogging::DEBUG, BasicLogging::ERROR, BasicLogging::FATAL, BasicLogging::INFO, BasicLogging::Levels, BasicLogging::UNKNOWN, BasicLogging::WARN
Instance Attribute Summary collapse
-
#has_user_conf ⇒ Object
readonly
Returns the value of attribute has_user_conf.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#log_level ⇒ Object
readonly
Returns the value of attribute log_level.
-
#user_configuration ⇒ Object
readonly
Returns the value of attribute user_configuration.
Attributes included from BasicLogging
Instance Method Summary collapse
-
#config_file ⇒ Object
returns the path to the configuration-file.
- #each_pair(&b) ⇒ Object
- #each_with_index(&b) ⇒ Object
-
#initialize ⇒ Configuration
constructor
initialize the singleton-instance and read the configuration- file.
-
#method_missing(method, *args) ⇒ Object
used to extract values from the confiuration-file.
- #read_config ⇒ Object
- #save_config(conf) ⇒ Object
- #size ⇒ Object
-
#temp_dir(options = {:remove => true}) ⇒ Object
Creates a temporary directory upon the first call but always returns its path Stolen from Alex Chaffee’s files-gem and stackoverflow.com/questions/1139265/what-is-the-best-way-to-get-a-temporary-directory-in-ruby-on-rails.
- #to_s ⇒ Object
Methods included from BasicLogging
is_muted?, #log, mute, #set_level, #set_target
Methods included from File_Checking
Constructor Details
#initialize ⇒ Configuration
initialize the singleton-instance and read the configuration- file.
35 36 37 38 39 40 41 |
# File 'lib/configuration.rb', line 35 def initialize read_config set_level(@conf["log level"]) set_target(@conf["log file"]) check_programs_installed end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
used to extract values from the confiuration-file
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/configuration.rb', line 60 def method_missing(method, *args) # debug('method missing ' << method.to_s << '(' << args.join(', ') << ')') key = method.to_s.gsub('_', ' ') if(@conf && key) co = @conf[key] # debug('returning conf-option ' << (co && !co.to_s.empty? ? co.to_s : 'nil')) return (co && !co.to_s.empty? ? co : nil) else error(trl("Unable to read an option from the configuration")) end end |
Instance Attribute Details
#has_user_conf ⇒ Object (readonly)
Returns the value of attribute has_user_conf.
149 150 151 |
# File 'lib/configuration.rb', line 149 def has_user_conf @has_user_conf end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
150 151 152 |
# File 'lib/configuration.rb', line 150 def keys @keys end |
#log_level ⇒ Object (readonly)
Returns the value of attribute log_level.
148 149 150 |
# File 'lib/configuration.rb', line 148 def log_level @log_level end |
#user_configuration ⇒ Object (readonly)
Returns the value of attribute user_configuration.
151 152 153 |
# File 'lib/configuration.rb', line 151 def user_configuration @user_configuration end |
Instance Method Details
#config_file ⇒ Object
returns the path to the configuration-file
73 74 75 |
# File 'lib/configuration.rb', line 73 def config_file @@config_file end |
#each_pair(&b) ⇒ Object
87 88 89 90 91 |
# File 'lib/configuration.rb', line 87 def each_pair(&b) @conf.each_pair do |k, v| yield(k, v) end end |
#each_with_index(&b) ⇒ Object
81 82 83 84 85 |
# File 'lib/configuration.rb', line 81 def each_with_index(&b) @conf.each_with_index do |pair, i| yield(pair, i) end end |
#read_config ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/configuration.rb', line 113 def read_config # replace the globally defined configuration file by a # user-provided version, if available. # -----> here @has_user_conf = user_conf # <------ msg = file_check(@@config_file, :exist, :readable) if(!msg) begin @conf = YAML::load_file(@@config_file) @keys = @conf.keys if(@keys.include?('log level') ) level = case @conf['log level'].downcase when 'debug' Logger::DEBUG when 'fatal' Logger::FATAL when 'error' Logger::ERROR when 'info' Logger::INFO when 'warn' Logger::WARN else Logger::UNKNOWN end else level = Logger::UNKNOWN end rescue Exception => ex msg = ex. end end end |
#save_config(conf) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/configuration.rb', line 43 def save_config(conf) yml = conf.to_yaml debug('yaml will be ' << yml) if (! Dir.exist?(@@USER_CONF_DIR) ) Dir.mkdir(@@USER_CONF_DIR) end File.open(@@USER_CONF, 'w') do |cf| cf << yml end end |
#size ⇒ Object
77 78 79 |
# File 'lib/configuration.rb', line 77 def size @conf.size end |
#temp_dir(options = {:remove => true}) ⇒ Object
Creates a temporary directory upon the first call but always returns its path Stolen from Alex Chaffee’s files-gem and stackoverflow.com/questions/1139265/what-is-the-best-way-to-get-a-temporary-directory-in-ruby-on-rails
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/configuration.rb', line 96 def temp_dir = {:remove => true} @temp_dir ||= begin debug('creating temp_dir') require 'tmpdir' require 'fileutils' # # called_from = File.basename caller.first.split(':').first, ".rb" called_from = CremeFraiche::prog_info[:app_name].gsub(' ', '_') debug('called_from is ' << called_from) path = File.join(Dir::tmpdir, "#{called_from}_#{Time.now.to_i}_#{rand(1000)}") debug('temp-dir path is ' << path) Dir.mkdir(path) at_exit {FileUtils.rm_rf(path) if File.exists?(path)} if [:remove] File.new path end end |
#to_s ⇒ Object
53 54 55 56 57 |
# File 'lib/configuration.rb', line 53 def to_s str = @conf.to_s debug(str) return str end |