Class: Configuration

Inherits:
Object
  • Object
show all
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::expand_path(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

Attributes included from BasicLogging

#target

Instance Method Summary collapse

Methods included from BasicLogging

is_muted?, #log, mute, #set_level, #set_target

Methods included from File_Checking

#file_check, file_check

Constructor Details

#initializeConfiguration

initialize the singleton-instance and read the configuration- file.



35
36
37
38
39
40
41
42
43
# File 'lib/configuration.rb', line 35

def initialize
  read_config
  v = @conf["log level"] 
  set_level( v ? BasicLogging::Levels[v.to_sym] : nil)
  v = @conf["log file"]
  set_target(v ? v : STDOUT)

  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



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/configuration.rb', line 62

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_confObject (readonly)

Returns the value of attribute has_user_conf.



151
152
153
# File 'lib/configuration.rb', line 151

def has_user_conf
  @has_user_conf
end

#keysObject (readonly)

Returns the value of attribute keys.



152
153
154
# File 'lib/configuration.rb', line 152

def keys
  @keys
end

#log_levelObject (readonly)

Returns the value of attribute log_level.



150
151
152
# File 'lib/configuration.rb', line 150

def log_level
  @log_level
end

#user_configurationObject (readonly)

Returns the value of attribute user_configuration.



153
154
155
# File 'lib/configuration.rb', line 153

def user_configuration
  @user_configuration
end

Instance Method Details

#config_fileObject

returns the path to the configuration-file



75
76
77
# File 'lib/configuration.rb', line 75

def config_file
  @@config_file
end

#each_pair(&b) ⇒ Object



89
90
91
92
93
# File 'lib/configuration.rb', line 89

def each_pair(&b)
  @conf.each_pair do |k, v|
    yield(k, v)
  end
end

#each_with_index(&b) ⇒ Object



83
84
85
86
87
# File 'lib/configuration.rb', line 83

def each_with_index(&b)
  @conf.each_with_index do |pair, i|
    yield(pair, i)
  end
end

#read_configObject



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
147
148
# File 'lib/configuration.rb', line 115

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.message
    end
  end
end

#save_config(conf) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/configuration.rb', line 45

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

#sizeObject



79
80
81
# File 'lib/configuration.rb', line 79

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/configuration.rb', line 98

def temp_dir options = {: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 options[:remove]
                  File.new path
                end
end

#to_sObject



55
56
57
58
59
# File 'lib/configuration.rb', line 55

def to_s
  str = @conf.to_s
  debug(str)
  return str
end