Class: Sqreen::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/sqreen/configuration.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(framework = nil) ⇒ Configuration

Returns a new instance of Configuration.



112
113
114
115
# File 'lib/sqreen/configuration.rb', line 112

def initialize(framework = nil)
  @framework = framework
  @config = default_values
end

Instance Method Details

#default_valuesObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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_fileObject



183
184
185
# 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_homeObject



178
179
180
181
# File 'lib/sqreen/configuration.rb', line 178

def find_user_home
  homes = %w[HOME HOMEPATH]
  homes.detect { |h| !ENV[h].nil? }
end

#from_environmentObject



149
150
151
152
153
154
155
156
157
158
# 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) ⇒ Object



130
131
132
# File 'lib/sqreen/configuration.rb', line 130

def get(name)
  @config[name.to_sym]
end

#load!Object



117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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