class DTK::Configuration

Constants

DEFAULT_CONF
DEVELOPMENT_CONF
EXTERNAL_APP_CONF

Public Class Methods

[](k) click to toggle source
# File lib/config/configuration.rb, line 50
def self.[](k)
  Configuration.instance.get(k)
end
get(name, default=nil) click to toggle source
# File lib/config/configuration.rb, line 46
def self.get(name, default=nil)
  Configuration.instance.get(name, default)
end
new() click to toggle source
# File lib/config/configuration.rb, line 54
def initialize
  # default configuration
  @cache = load_configuration_to_hash(File.expand_path("../#{DEFAULT_CONF}",__FILE__))

  # we will not use local.conf from gemfile because client.conf is required so this is deprecated
  if File.exist?(File.expand_path("../#{DEVELOPMENT_CONF}",__FILE__))
    local_configuration = load_configuration_to_hash(File.expand_path("../#{DEVELOPMENT_CONF}",__FILE__))
    # we override only values from local configuration
    # that way developer does not have updates its local configuration all the time
    @cache.merge!(local_configuration)
    # if we have loaded local configuration we will not check external
    return
  end

  # We load this if there is no local configuration
  external_file_location = File.join(::DTK::Client::OsUtil.dtk_local_folder(), "#{EXTERNAL_APP_CONF}")

  if File.exist?(external_file_location)
    external_configuration = load_configuration_to_hash(external_file_location)
    @cache.merge!(external_configuration)
  end
end

Public Instance Methods

get(name, default=nil) click to toggle source
# File lib/config/configuration.rb, line 77
def get(name, default=nil)
  return @cache[name.to_s] || default
end

Private Instance Methods

load_configuration_to_hash(path_to_file) click to toggle source
# File lib/config/configuration.rb, line 83
def load_configuration_to_hash(path_to_file)
  configuration = Hash[*File.read(path_to_file).gsub(/#.+/,'').strip().gsub(/( |\t)+$/,'').split(/[=\n\r\r\n]+/)]

  # check for types
  return configuration.each do |k,v|
    case v
      when /^(true|false)$/ 
        configuration[k] = v.eql?('true') ? true : false
      when /^[0-9]+$/
        configuration[k] = v.to_i
      when /^[0-9\.]+$/
        configuration[k] = v.to_f
    end
  end
end