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.4"
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
30
31
32
|
# File 'lib/config_reader.rb', line 30
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
|
107
108
109
110
|
# File 'lib/config_reader.rb', line 107
def configure
self.configuration ||= Configuration.new
yield(configuration)
end
|
.deep_merge!(hash, other_hash) ⇒ Object
116
117
118
119
120
121
122
123
124
|
# File 'lib/config_reader.rb', line 116
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
|
.dig(*args) ⇒ Object
20
21
22
23
24
|
# File 'lib/config_reader.rb', line 20
def dig(*args)
args.map!(&:to_sym) if args.respond_to?(:map!)
config.dig(*args)
end
|
.find_config ⇒ Object
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/config_reader.rb', line 34
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
57
58
59
|
# File 'lib/config_reader.rb', line 57
def inspect
puts config.inspect
end
|
.load_config ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/config_reader.rb', line 61
def load_config
raise "No config file set" unless find_config
conf =
if defined?(ERB)
YAML.load(ERB.new(File.read(find_config)).result)
else
YAML.load_file(File.read(find_config))
end
raise "No config found" unless conf
conf
end
|
.load_sekrets ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/config_reader.rb', line 76
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
warn "You specified a sekrets_file, but the sekrets gem isn't available."
end
end
sekrets
end
|
.merge_configs(conf, sekrets) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/config_reader.rb', line 92
def merge_configs(conf, sekrets)
env = configuration.environment
defaults = conf["defaults"]
if sekrets && sekrets["defaults"]
deep_merge!(defaults, sekrets["defaults"])
end
deep_merge!(defaults, conf[env]) if conf[env]
deep_merge!(defaults, sekrets[env]) if sekrets && sekrets[env]
MagicHash.convert_hash(defaults, configuration.ignore_missing_keys)
end
|
.method_missing(key, *args, &block) ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/config_reader.rb', line 45
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
26
27
28
|
# File 'lib/config_reader.rb', line 26
def reload
merge_configs(load_config, load_sekrets)
end
|
.respond_to_missing?(m) ⇒ Boolean
53
54
55
|
# File 'lib/config_reader.rb', line 53
def respond_to_missing?(m, *)
config.key?(m)
end
|