class PostRunner::RuntimeConfig
Simple class to manage runtime configuration options which are persisted in a YAML file.
Public Class Methods
new(dir)
click to toggle source
Create a new RC object. @param dir [String] the directory to hold the config.yml file.
# File lib/postrunner/RuntimeConfig.rb, line 25 def initialize(dir) create_directory(dir, 'application data') @options = { :version => '0.0.0', :unit_system => :metric, :import_dir => nil, :data_dir => dir, :html_dir => File.join(dir, 'html') } @config_file = File.join(dir, 'config.yml') load_options if File.exist?(@config_file) end
Public Instance Methods
[](name)
click to toggle source
Shortcut for get_option. @param name [Symbol] the name of the config option. @return [Object] the value of the config option.
# File lib/postrunner/RuntimeConfig.rb, line 42 def [](name) get_option(name) end
create_directory(dir, name)
click to toggle source
Ensure that the requested directory exists.
# File lib/postrunner/RuntimeConfig.rb, line 62 def create_directory(dir, name) return if Dir.exist?(dir) Log.info "Creating #{name} directory #{dir}" begin Dir.mkdir(dir) rescue StandardError Log.fatal "Cannot create #{name} directory #{dir}: #{$!}" end end
get_option(name)
click to toggle source
Get a config option value. @param name [Symbol] the name of the config option. @return [Object] the value of the config option.
# File lib/postrunner/RuntimeConfig.rb, line 49 def get_option(name) @options[name] end
set_option(name, value)
click to toggle source
Set a config option and update the RC file. @param name [Symbol] The name of the config option. @param value [Object] The value of the config option.
# File lib/postrunner/RuntimeConfig.rb, line 56 def set_option(name, value) @options[name] = value save_options end
Private Instance Methods
load_options()
click to toggle source
# File lib/postrunner/RuntimeConfig.rb, line 75 def load_options begin opts = YAML::load_file(@config_file) rescue IOError Log.error "Cannot load config file '#{@config_file}': #{$!}" end # Merge the loaded options into the @options hash. opts.each { |key, value| @options[key] = value } end
save_options()
click to toggle source
# File lib/postrunner/RuntimeConfig.rb, line 85 def save_options begin BackedUpFile.write(@config_file, @options.to_yaml) Log.info "Runtime config file '#{@config_file}' written" rescue Log.error "Cannot write config file '#{@config_file}': #{$!}" end end