46 lines
1.3 KiB
Ruby
46 lines
1.3 KiB
Ruby
require 'settingslogic'
|
|
require 'uri'
|
|
|
|
# Set application config
|
|
class AppConfig < Settingslogic
|
|
source "#{Dir.pwd}/config/application.yml"
|
|
namespace Rails.env
|
|
suppress_errors true
|
|
|
|
#
|
|
# Gets the log level as a symbol (one of :debug, :info, :warn, :error, etc.)
|
|
#
|
|
def self.log_level(default = :info)
|
|
self.log.level.to_sym rescue default
|
|
end
|
|
|
|
#
|
|
# Gets the log level as an integer (debug: 0, info: 1, etc.)
|
|
#
|
|
def self.numeric_log_level(default = Logger::INFO)
|
|
Logger.const_get(log_level(nil).upcase) rescue default
|
|
end
|
|
|
|
# Safe retrieval of an option value with options:
|
|
# :default specifies default value
|
|
# :warn_if_missing true: emits warning to logger if setting missing
|
|
# :integer true: forces default value if setting is not an integer
|
|
def self.safe_get(attr, options = {})
|
|
val = attr.to_s.split('.').reduce(self) { |config, attr| config.send attr }
|
|
raise if options[:integer] && val.to_i.to_s != val
|
|
val
|
|
rescue
|
|
default = options[:default]
|
|
logger.warn("No config for #{attr}. #{ default ? "Using default: #{default}" : 'No default'}") if options[:warn_if_missing]
|
|
default
|
|
end
|
|
|
|
def self.get_mandatory(attr)
|
|
safe_get(attr) || raise(StandardError, "No such setting #{attr}")
|
|
end
|
|
|
|
def self.logger
|
|
Rails.logger
|
|
end
|
|
end
|