class Enc::Config
Attributes
config_file[R]
Public Class Methods
new(config_file=nil, options={}, use_file=true)
click to toggle source
# File lib/enc/config.rb, line 8 def initialize(config_file=nil, options={}, use_file=true) @config_file = get_default_config(config_file) @options = options.each { |k,v| {k.to_sym => v} } @use_file = use_file end
Public Instance Methods
config_file_exists()
click to toggle source
# File lib/enc/config.rb, line 26 def config_file_exists File.exist?(@config_file) end
create!()
click to toggle source
# File lib/enc/config.rb, line 30 def create! raise InvalidConfiguration, 'Configuration requires host, username and password to be present.' unless @options[:host] and @options[:username] and @options[:password] File.open(@config_file, 'w') {|f| f.write @options.to_yaml } end
delete!()
click to toggle source
# File lib/enc/config.rb, line 36 def delete! File.delete(@config_file) if File.exist?(@config_file) end
get(key)
click to toggle source
# File lib/enc/config.rb, line 14 def get(key) load[key.to_sym] end
load()
click to toggle source
# File lib/enc/config.rb, line 18 def load if @options.empty? @options = YAML.load_file(@config_file).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} end set_defaults @options end
Protected Instance Methods
get_default_config(config_file=nil)
click to toggle source
Control the default config location order here. We need to assume at least this much so we know where to load the main collins config file.
# File lib/enc/config.rb, line 50 def get_default_config(config_file=nil) if config_file.nil? or config_file == '' return "#{ENV['HOME']}/.collins.yaml" if File.exists?("#{ENV['HOME']}/.collins.yaml") return'/var/lib/collins/.collins.yaml' if File.exists?('/var/lib/collins/.collins.yaml') raise ConfigurationDoesNotExist, 'Could not find configuration in ~/.collins.yaml or /var/lib/collins/.collins.yaml.' else raise ConfigurationDoesNotExist, "Config file #{config_file} does not exist" if (not File.exists?(config_file) and @use_file) config_file end end
set_defaults()
click to toggle source
# File lib/enc/config.rb, line 42 def set_defaults @options[:timeout] = 30 unless @options.has_key?(:timeout) @options[:jive_enc_cache_dir] = '/var/tmp/enc_cache' unless @options.has_key?(:jive_enc_cache_dir) @options[:jive_enc_log_file] = '/var/log/enc.log' unless @options.has_key?(:jive_enc_log_file) end