Class: ConfigReader
- Inherits:
-
Object
show all
- Defined in:
- lib/config_reader.rb,
lib/config_reader/version.rb,
lib/config_reader/magic_hash.rb
Defined Under Namespace
Classes: Configuration, MagicHash
Constant Summary
collapse
- VERSION =
"2.0.0"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
14
15
16
|
# File 'lib/config_reader.rb', line 14
def configuration
@configuration
end
|
Class Method Details
.[](key) ⇒ Object
25
26
27
|
# File 'lib/config_reader.rb', line 25
def [](key)
config[key.to_sym]
end
|
.config ⇒ Object
16
17
18
19
|
# File 'lib/config_reader.rb', line 16
def config
@config = nil unless defined?(@config)
@config ||= reload
end
|
93
94
95
96
|
# File 'lib/config_reader.rb', line 93
def configure
self.configuration ||= Configuration.new
yield(configuration)
end
|
.find_config ⇒ Object
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/config_reader.rb', line 29
def find_config
return configuration.config_file if File.exist?(configuration.config_file)
%w( . config ).each do |dir|
config_file = File.join(dir, configuration.config_file)
return config_file if File.exist?(config_file)
end
nil
end
|
.inspect ⇒ Object
48
49
50
|
# File 'lib/config_reader.rb', line 48
def inspect
puts config.inspect
end
|
.load_config ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/config_reader.rb', line 52
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_sekrets ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/config_reader.rb', line 66
def load_sekrets
sekrets = {}
if configuration.sekrets_file
begin
require 'sekrets'
sekrets = ::Sekrets.settings_for(configuration.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(conf, sekrets) ⇒ Object
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/config_reader.rb', line 82
def merge_configs(conf, sekrets)
env = configuration.environment
_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]
MagicHash.convert_hash(_conf, configuration.ignore_missing_keys)
end
|
.method_missing(key, *args, &block) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/config_reader.rb', line 40
def method_missing(key, *args, &block)
if key.to_s.end_with?('=')
raise ArgumentError.new('ConfigReader is immutable')
end
config[key] || nil
end
|
.reload ⇒ Object
21
22
23
|
# File 'lib/config_reader.rb', line 21
def reload
merge_configs(load_config, load_sekrets)
end
|