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.2"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
13
14
15
|
# File 'lib/config_reader.rb', line 13
def configuration
@configuration
end
|
Class Method Details
.[](key) ⇒ Object
24
25
26
|
# File 'lib/config_reader.rb', line 24
def [](key)
config[key.to_sym]
end
|
.config ⇒ Object
15
16
17
18
|
# File 'lib/config_reader.rb', line 15
def config
@config = nil unless defined?(@config)
@config ||= reload
end
|
92
93
94
95
|
# File 'lib/config_reader.rb', line 92
def configure
self.configuration ||= Configuration.new
yield(configuration)
end
|
.deep_merge!(hash, other_hash) ⇒ Object
101
102
103
104
105
106
107
108
109
|
# File 'lib/config_reader.rb', line 101
def deep_merge!(hash, other_hash)
hash.merge!(other_hash) do |key, this_val, other_val|
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
deep_merge!(this_val, other_val)
else
other_val
end
end
end
|
.find_config ⇒ Object
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/config_reader.rb', line 28
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
47
48
49
|
# File 'lib/config_reader.rb', line 47
def inspect
puts config.inspect
end
|
.load_config ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/config_reader.rb', line 51
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/config_reader.rb', line 65
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
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/config_reader.rb', line 81
def merge_configs(conf, sekrets)
env = configuration.environment
_conf = conf['defaults']
deep_merge!(_conf, sekrets['defaults']) if sekrets && sekrets['defaults']
deep_merge!(_conf, conf[env]) if conf[env]
deep_merge!(_conf, sekrets[env]) if sekrets && sekrets[env]
MagicHash.convert_hash(_conf, configuration.ignore_missing_keys)
end
|
.method_missing(key, *args, &block) ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/config_reader.rb', line 39
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
20
21
22
|
# File 'lib/config_reader.rb', line 20
def reload
merge_configs(load_config, load_sekrets)
end
|