Class: D13n::Configuration::EnvironmentSource

Inherits:
DottedHash
  • Object
show all
Defined in:
lib/d13n/configuration/environment_source.rb

Constant Summary collapse

SUPPORTED_PREFIXES =
Proc.new {/^#{D13n.app_name}_/i}
SPECIAL_CASE_KEYS =
[
  "#{D13n.app_prefix}_LOG"  # read by set_log_file
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DottedHash

#inspect, symbolize, #to_hash

Constructor Details

#initializeEnvironmentSource

Returns a new instance of EnvironmentSource.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/d13n/configuration/environment_source.rb', line 10

def initialize
  set_log_file
  set_config_file

  @type_map = {}

  DEFAULTS.each do |config_setting, value|
    self.type_map[config_setting] = value[:type]
  end

  set_values_from_app_environment_variables
end

Instance Attribute Details

#type_mapObject

Returns the value of attribute type_map.



8
9
10
# File 'lib/d13n/configuration/environment_source.rb', line 8

def type_map
  @type_map
end

Instance Method Details

#collect_app_environment_variable_keysObject



85
86
87
# File 'lib/d13n/configuration/environment_source.rb', line 85

def collect_app_environment_variable_keys
  ENV.keys.select { |key| key.match(SUPPORTED_PREFIXES.call) }
end

#convert_environment_key_to_config_key(key) ⇒ Object



80
81
82
# File 'lib/d13n/configuration/environment_source.rb', line 80

def convert_environment_key_to_config_key(key)
  stripped_key = key.gsub(SUPPORTED_PREFIXES.call, '').downcase.to_sym
end

#set_config_fileObject



35
36
37
38
# File 'lib/d13n/configuration/environment_source.rb', line 35

def set_config_file
  env_config = "#{D13n.app_prefix}_CONFIG"
  self[:config_path] = ENV[env_config] if ENV[env_config]
end

#set_key_by_type(config_key, environment_key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/d13n/configuration/environment_source.rb', line 54

def set_key_by_type(config_key, environment_key)
  value = ENV[environment_key]
  type = self.type_map[config_key]

  if type == String
    self[config_key] = value
  elsif type == Integer
    self[config_key] = value.to_i
  elsif type == Float
    self[config_key] = value.to_f
  elsif type == Symbol
    self[config_key] = value.to_sym
  elsif type == Array
    self[config_key] = value.split(/\s*,\s*/)
  elsif type == D13n::Configuration::Boolean
    if value =~ /false|off|no/i
      self[config_key] = false
    elsif value != nil
      self[config_key] = true
    end
  else
    D13n.logger.info("#{environment_key} does not have a corresponding configuration setting (#{config_key} does not exist).")
    self[config_key] = value
  end
end

#set_log_fileObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/d13n/configuration/environment_source.rb', line 23

def set_log_file
  env_log = "#{D13n.app_prefix}_LOG"
  if ENV[env_log]
    if ENV[env_log].upcase == 'STDOUT'
      self[:log_file_path] = self[:log_file_name] = 'STDOUT'
    else
      self[:log_file_path] = File.dirname(ENV[env_log])
      self[:log_file_name] = File.basename(ENV[env_log])
    end
  end
end

#set_value_from_app_environment_variable(key) ⇒ Object



49
50
51
52
# File 'lib/d13n/configuration/environment_source.rb', line 49

def set_value_from_app_environment_variable(key)
  config_key = convert_environment_key_to_config_key(key)
  set_key_by_type(config_key, key)
end

#set_values_from_app_environment_variablesObject



40
41
42
43
44
45
46
47
# File 'lib/d13n/configuration/environment_source.rb', line 40

def set_values_from_app_environment_variables
  app_env_var_keys = collect_app_environment_variable_keys

  app_env_var_keys.each do |key|
    next if SPECIAL_CASE_KEYS.include?(key.upcase)
    set_value_from_app_environment_variable(key)
  end
end