Class: ConfigReader

Inherits:
Object
  • Object
show all
Defined in:
lib/config_reader.rb,
lib/config_reader/version.rb,
lib/config_reader/magic_hash.rb

Defined Under Namespace

Classes: MagicHash

Constant Summary collapse

VERSION =
"1.0.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



14
15
16
# File 'lib/config_reader.rb', line 14

def config
  @config
end

.config_fileObject

Returns the value of attribute config_file.



14
15
16
# File 'lib/config_reader.rb', line 14

def config_file
  @config_file
end

.sekrets_fileObject

Returns the value of attribute sekrets_file.



14
15
16
# File 'lib/config_reader.rb', line 14

def sekrets_file
  @sekrets_file
end

Class Method Details

.[](key) ⇒ Object



29
30
31
# File 'lib/config_reader.rb', line 29

def [](key)
  config[key.to_sym]
end

.find_configObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/config_reader.rb', line 33

def find_config
  return @config_file if File.exist?(@config_file)

  %w( . config ).each do |dir|
    config_file = File.join(dir, @config_file)
    return config_file if File.exist?(config_file)
  end

  ''
end

.find_envObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/config_reader.rb', line 56

def find_env
  if defined?(Rails) && Rails.respond_to?(:stage)
    Rails.stage
  elsif defined?(Rails) && Rails.env
    Rails.env
  elsif defined?(RAILS_ENV)
    RAILS_ENV
  elsif defined?(Padrino) && Padrino.env
    Padrino.env.to_s
  elsif defined?(PADRINO_ENV)
    PADRINO_ENV
  elsif ENV['RACK_ENV']
    ENV['RACK_ENV']
  elsif defined?(APP_ENV)
    APP_ENV
  end
end

.inspectObject



52
53
54
# File 'lib/config_reader.rb', line 52

def inspect
  puts config.inspect
end

.load_configObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/config_reader.rb', line 74

def load_config
  raise 'No config file set' unless find_config

  if defined?(ERB)
    conf = YAML.load(ERB.new(File.open(find_config).read).result)
  else
    conf = YAML.load(File.open(find_config).read)
  end

  raise 'No config found' unless conf

  conf
end

.load_sekretsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/config_reader.rb', line 88

def load_sekrets
  sekrets = {}

  if @sekrets_file
    begin
      require 'sekrets'
      sekrets = ::Sekrets.settings_for(@sekrets_file)
      raise 'No sekrets found' unless sekrets
    rescue LoadError
      $stderr.puts "You specified a sekrets_file, but the sekrets gem isn't available."
    end
  end

  sekrets
end

.merge_configs(env, conf, sekrets) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/config_reader.rb', line 104

def merge_configs(env, conf, sekrets)
  _conf = conf['defaults']
  _conf.deep_merge!(sekrets['defaults']) if sekrets && sekrets['defaults']
  _conf.deep_merge!(conf[env]) if conf[env]
  _conf.deep_merge!(sekrets[env]) if sekrets && sekrets[env]
  ConfigReader::MagicHash.convert_hash(_conf)
end

.method_missing(key, *args, &block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/config_reader.rb', line 44

def method_missing(key, *args, &block)
  if key.to_s.end_with?('=')
    raise ArgumentError.new('ConfigReader is immutable')
  end

  config[key] || nil
end

.reloadObject



25
26
27
# File 'lib/config_reader.rb', line 25

def reload
  merge_configs(find_env, load_config, load_sekrets)
end