Class: Confg::Configuration
- Inherits:
-
Object
- Object
- Confg::Configuration
show all
- Defined in:
- lib/confg/configuration.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#fetch(key, &block) ⇒ Object
-
#get(key) ⇒ Object
(also: #[])
-
#initialize(env: Confg.env, root: Confg.root) ⇒ Configuration
constructor
A new instance of Configuration.
-
#inspect ⇒ Object
-
#key?(key) ⇒ Boolean
-
#load_env(prefix: "CONFG_", separator: "__") ⇒ Object
-
#load_key(key, yaml_loader_options = {}) ⇒ Object
-
#load_yaml(path, yaml_loader_options = {}, key: nil, ignore_env: false) ⇒ Object
(also: #load_yml)
-
#merge(other) ⇒ Object
(also: #merge!)
-
#method_missing(key, *args, **kwargs, &block) ⇒ Object
-
#open(key) {|inner| ... } ⇒ Object
-
#respond_to_missing?(key, include_private = false) ⇒ Boolean
-
#set(key, value = nil) ⇒ Object
(also: #[]=)
-
#tmp(key, value) ⇒ Object
-
#to_h ⇒ Object
Constructor Details
#initialize(env: Confg.env, root: Confg.root) ⇒ Configuration
Returns a new instance of Configuration.
10
11
12
13
14
|
# File 'lib/confg/configuration.rb', line 10
def initialize(env: Confg.env, root: Confg.root)
@confg_env = env.to_s
@confg_root = Pathname.new(root)
@confg_data = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *args, **kwargs, &block) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/confg/configuration.rb', line 126
def method_missing(key, *args, **kwargs, &block)
key = key.to_s
return set(key[0...-1], args[0]) if key.end_with?("=")
return fetch(key) if key?(key)
begin
confg_data.send(key, *args, **kwargs, &block)
rescue NoMethodError => e
raise KeyError, "Unrecognized key `#{key}`", e.backtrace
end
end
|
Instance Attribute Details
#confg_data ⇒ Object
Returns the value of attribute confg_data.
8
9
10
|
# File 'lib/confg/configuration.rb', line 8
def confg_data
@confg_data
end
|
#confg_env ⇒ Object
Returns the value of attribute confg_env.
8
9
10
|
# File 'lib/confg/configuration.rb', line 8
def confg_env
@confg_env
end
|
#confg_root ⇒ Object
Returns the value of attribute confg_root.
8
9
10
|
# File 'lib/confg/configuration.rb', line 8
def confg_root
@confg_root
end
|
Instance Method Details
#fetch(key, &block) ⇒ Object
69
70
71
|
# File 'lib/confg/configuration.rb', line 69
def fetch(key, &block)
confg_data.fetch(key.to_s, &block)
end
|
#get(key) ⇒ Object
Also known as:
[]
64
65
66
|
# File 'lib/confg/configuration.rb', line 64
def get(key)
fetch(key) { nil }
end
|
#inspect ⇒ Object
16
17
18
|
# File 'lib/confg/configuration.rb', line 16
def inspect
"#<#{self.class.name} #{confg_data.inspect}>"
end
|
#key?(key) ⇒ Boolean
60
61
62
|
# File 'lib/confg/configuration.rb', line 60
def key?(key)
confg_data.key?(key.to_s)
end
|
#load_env(prefix: "CONFG_", separator: "__") ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/confg/configuration.rb', line 35
def load_env(prefix: "CONFG_", separator: "__")
overrides = {}
ENV.each do |key, value|
next unless key.start_with?(prefix)
segments = key.delete_prefix(prefix).downcase.split(separator)
current = overrides
segments[0...-1].each do |segment|
current = (current[segment] ||= {})
end
current[segments.last] = value
end
merge(overrides)
end
|
#load_key(key, yaml_loader_options = {}) ⇒ Object
93
94
95
96
|
# File 'lib/confg/configuration.rb', line 93
def load_key(key,yaml_loader_options = {})
load_yaml(key, yaml_loader_options, key: key)
end
|
#load_yaml(path, yaml_loader_options = {}, key: nil, ignore_env: false) ⇒ Object
Also known as:
load_yml
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/confg/configuration.rb', line 98
def load_yaml(path, yaml_loader_options = {}, key: nil, ignore_env: false)
found_path = find_config_yaml(path)
raise ArgumentError, "#{path} could not be found" if found_path.nil?
ctxt = ::Confg::ErbContext.new
raw_content = ::File.read(found_path)
erb_content = ctxt.evaluate(raw_content)
yaml_content = ::YAML.safe_load(erb_content, **yaml_loader_options.merge(aliases: true))
unless ignore_env
yaml_content = yaml_content[confg_env] if confg_env && yaml_content.is_a?(::Hash) && yaml_content.key?(confg_env)
end
if key
set(key, yaml_content)
else
if yaml_content.is_a?(Array)
raise "A key must be provided to load the file at: #{found_path}"
else
yaml_content.each do |k, v|
set(k, v)
end
end
end
end
|
#merge(other) ⇒ Object
Also known as:
merge!
28
29
30
31
32
|
# File 'lib/confg/configuration.rb', line 28
def merge(other)
other.each_pair do |k, v|
set(k, v)
end
end
|
#open(key) {|inner| ... } ⇒ Object
87
88
89
90
91
|
# File 'lib/confg/configuration.rb', line 87
def open(key)
inner = get(key) || spawn_child
yield(inner)
set(key, inner)
end
|
#respond_to_missing?(key, include_private = false) ⇒ Boolean
138
139
140
141
142
143
144
|
# File 'lib/confg/configuration.rb', line 138
def respond_to_missing?(key, include_private = false)
key = key.to_s
return true if key.end_with?("=")
return true if key?(key)
confg_data.respond_to?(key, include_private)
end
|
#set(key, value = nil) ⇒ Object
Also known as:
[]=
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/confg/configuration.rb', line 73
def set(key, value = nil)
confg_data[key.to_s] = case value
when ::Hash
open(key) do |child|
value.each_pair do |k, v|
child.set(k, v)
end
end
else
value
end
end
|
#tmp(key, value) ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/confg/configuration.rb', line 20
def tmp(key, value)
initial = get(key)
set(key, value)
yield
ensure
set(key, initial)
end
|
#to_h ⇒ Object
54
55
56
57
58
|
# File 'lib/confg/configuration.rb', line 54
def to_h
confg_data.transform_values do |v|
v.is_a?(self.class) ? v.to_h : v
end
end
|