Class: ConfigReader
- Inherits:
-
Object
show all
- Defined in:
- lib/config_reader.rb,
lib/config_reader/version.rb,
lib/config_reader/config_hash.rb
Defined Under Namespace
Classes: ConfigHash, Configuration
Constant Summary
collapse
- VERSION =
"3.0.2"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.envs ⇒ Object
Returns the value of attribute envs.
18
19
20
|
# File 'lib/config_reader.rb', line 18
def envs
@envs
end
|
Class Method Details
.[](key) ⇒ Object
20
21
22
|
# File 'lib/config_reader.rb', line 20
def [](key)
config[key.to_sym]
end
|
.config ⇒ Object
24
25
26
27
|
# File 'lib/config_reader.rb', line 24
def config
@config = nil unless defined?(@config)
@config ||= reload
end
|
.configuration ⇒ Object
29
30
31
|
# File 'lib/config_reader.rb', line 29
def configuration
@configuration ||= Configuration.new
end
|
33
34
35
36
|
# File 'lib/config_reader.rb', line 33
def configure
self.configuration ||= Configuration.new
yield(configuration)
end
|
.deep_merge(hash, other_hash) ⇒ Object
38
39
40
41
42
43
44
45
46
|
# File 'lib/config_reader.rb', line 38
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
hash[key] = other_val
end
end
end
|
.dig(*args) ⇒ Object
48
49
50
51
52
|
# File 'lib/config_reader.rb', line 48
def dig(*args)
args.map!(&:to_sym) if args.respond_to?(:map!)
config.dig(*args)
end
|
.find_config ⇒ Object
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/config_reader.rb', line 54
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
65
66
67
|
# File 'lib/config_reader.rb', line 65
def inspect
config.inspect
end
|
.load_config ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/config_reader.rb', line 69
def load_config
raise "No config file set" unless find_config
conf = load_yaml
raise "No config found" unless conf
conf
end
|
.load_sekrets ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/config_reader.rb', line 79
def load_sekrets
if configuration.sekrets_file
if !defined?(::Sekrets)
raise ArgumentError,
"You specified a sekrets_file, but the sekrets gem isn't available."
else
::Sekrets.settings_for(configuration.sekrets_file) ||
raise("No sekrets found")
end
end
end
|
.load_yaml ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/config_reader.rb', line 91
def load_yaml
permitted_classes = configuration.permitted_classes.to_a + [Symbol]
if defined?(ERB)
Psych.safe_load(
ERB.new(File.read(find_config)).result,
aliases: true,
permitted_classes: permitted_classes
)
else
Psych.safe_load_file(
File.read(find_config),
aliases: true,
permitted_classes: permitted_classes
)
end
end
|
.merge_all_configs(conf, defaults, sekrets) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/config_reader.rb', line 109
def merge_all_configs(conf, defaults, sekrets)
@envs = {}
(conf.keys - ["defaults"]).each do |env|
key_hash = deep_merge(defaults.dup, conf[env]) if conf[env]
key_hash = deep_merge(key_hash, sekrets[env]) if sekrets&.[](env)
@envs[env] = ConfigHash.convert_hash(
key_hash,
configuration.ignore_missing_keys
)
end
end
|
.merge_configs(conf, sekrets) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/config_reader.rb', line 123
def merge_configs(conf, sekrets)
defaults = conf["defaults"]
if sekrets&.[]("defaults")
defaults = deep_merge(defaults, sekrets["defaults"])
end
merge_all_configs(conf, defaults, sekrets)
@envs[configuration.environment]
end
|
.method_missing(key, *_args, &_block) ⇒ Object
135
136
137
138
139
140
141
|
# File 'lib/config_reader.rb', line 135
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
143
144
145
|
# File 'lib/config_reader.rb', line 143
def reload
merge_configs(load_config, load_sekrets)
end
|
.respond_to_missing?(m) ⇒ Boolean
147
148
149
|
# File 'lib/config_reader.rb', line 147
def respond_to_missing?(m, *)
config.key?(m)
end
|