class Sqreen::Configuration

Class to access configurations variables This try to load environment by different ways.

  1. By file:

a. From path in environment variable SQREEN_CONFIG_FILE
b. From path in #{Rails.root}/config/sqreen.yml
c. From home in ~/.sqreen.yml
  1. From the environment, which overrides whatever result we found in 1.

Public Class Methods

new(framework = nil) click to toggle source
# File lib/sqreen/configuration.rb, line 112
def initialize(framework = nil)
  @framework = framework
  @config = default_values
end

Public Instance Methods

default_values() click to toggle source
# File lib/sqreen/configuration.rb, line 134
def default_values
  res = {}
  Sqreen::CONFIG_DESCRIPTION.each do |param|
    name      = param[:name]
    value     = param[:default]
    choices   = param[:choices]
    if choices && !choices.include?(value)
      msg = format("Invalid value '%s' for env '%s' (allowed: %s)", value, name, choices)
      raise Sqreen::Exception, msg
    end
    res[name] = param[:convert] ? send(param[:convert], value) : value
  end
  res
end
find_configuration_file() click to toggle source
# File lib/sqreen/configuration.rb, line 183
def find_configuration_file
  config_file_from_env || local_config_file || config_file_from_home
end
find_user_home() click to toggle source
# File lib/sqreen/configuration.rb, line 178
def find_user_home
  homes = %w[HOME HOMEPATH]
  homes.detect { |h| !ENV[h].nil? }
end
from_environment() click to toggle source
# File lib/sqreen/configuration.rb, line 149
def from_environment
  res = {}
  Sqreen::CONFIG_DESCRIPTION.each do |param|
    name      = param[:name]
    value     = ENV[param[:env].to_s]
    next unless value
    res[name] = param[:convert] ? send(param[:convert], value) : value
  end
  res
end
get(name) click to toggle source
# File lib/sqreen/configuration.rb, line 130
def get(name)
  @config[name.to_sym]
end
load!() click to toggle source
# File lib/sqreen/configuration.rb, line 117
def load!
  path = find_configuration_file
  if path
    file_config = parse_configuration_file(path)
    @config.merge!(file_config)
  else
    Sqreen.log.warn("could not find sqreen configuration file")
  end

  env_config = from_environment
  @config.merge!(env_config)
end
parse_configuration_file(path) click to toggle source
# File lib/sqreen/configuration.rb, line 160
def parse_configuration_file(path)
  yaml = YAML.load(ERB.new(File.read(path)).result)
  return {} unless yaml.is_a?(Hash)
  if @framework
    env = @framework.framework_infos[:environment]
    yaml = yaml[env] if env && yaml[env].is_a?(Hash)
  end
  res = {}
  # hash keys loaded by YAML are strings instead of symbols
  Sqreen::CONFIG_DESCRIPTION.each do |param|
    name      = param[:name]
    value     = yaml[name.to_s]
    next unless value
    res[name] = param[:convert] ? send(param[:convert], value) : value
  end
  res
end

Protected Instance Methods

config_file_from_env() click to toggle source
# File lib/sqreen/configuration.rb, line 189
def config_file_from_env
  return unless (path = ENV[Sqreen::CONFIG_FILE_BY_ENV])

  path = File.expand_path(path)

  unless File.exist?(path)
    Sqreen.log.warn("could not find sqreen configuration file provided by #{Sqreen::CONFIG_FILE_BY_ENV}: '#{path}'")
  end

  Sqreen.log.info("using config file provided by #{Sqreen::CONFIG_FILE_BY_ENV}: '#{path}'")

  path
end
config_file_from_home() click to toggle source
# File lib/sqreen/configuration.rb, line 213
def config_file_from_home
  home = find_user_home
  return unless home
  path = File.join(ENV[home], '.' + CONFIG_FILE_NAME)
  return path if File.exist?(path)
end
local_config_file() click to toggle source
# File lib/sqreen/configuration.rb, line 203
def local_config_file
  if @framework && @framework.root
    path = File.join(@framework.root.to_s, 'config', CONFIG_FILE_NAME)
    return path if File.exist?(path)
  else
    path = File.expand_path(File.join('config', 'sqreen.yml'))
    return path if File.exist?(path)
  end
end
to_bool(value) click to toggle source
# File lib/sqreen/configuration.rb, line 220
def to_bool(value)
  Sqreen::to_bool(value)
end
to_int(value) click to toggle source
# File lib/sqreen/configuration.rb, line 224
def to_int(value)
  Sqreen::to_int(value)
end
to_sym(value) click to toggle source
# File lib/sqreen/configuration.rb, line 228
def to_sym(value)
  Sqreen::to_sym(value)
end