Class: Rbcli::UserConf::Env
Instance Attribute Summary
Attributes inherited from Backend
#loaded, #type
Instance Method Summary
collapse
Methods inherited from Backend
#annotate!, create, inherited, types
Constructor Details
#initialize(prefix, type) ⇒ Env
Returns a new instance of Env.
11
12
13
14
15
|
# File 'lib/rbcli/components/config/backends/env.rb', line 11
def initialize prefix, type
@prefix = prefix
@type = type.to_s.capitalize
@loaded = false
end
|
Instance Method Details
#exist? ⇒ Boolean
60
61
62
|
# File 'lib/rbcli/components/config/backends/env.rb', line 60
def exist?
true
end
|
#load(defaults = nil) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/rbcli/components/config/backends/env.rb', line 17
def load defaults = nil
vars = ENV.select { |k, v| k.match(/^#{@prefix}_.+/i) && !k.match(/^_/) }
if @prefix.nil? && !defaults.nil? && !defaults.empty?
lowercase_default_keys = defaults.keys.map { |k| k.downcase.to_sym }
vars = vars.merge ENV.select { |k, v| lowercase_default_keys.include?(k.downcase.to_sym) }
end
vars = vars.map { |k, v| [k.split('_', 2).last.downcase, v] }.to_h
final_vars = Hash.new
vars.each do |key, value|
deep_assign(final_vars, key.split('_').map { |k| k.to_sym }, translate_value(value))
end
@loaded = true
final_vars
end
|
#parse_defaults(defaults) ⇒ Object
46
47
48
49
50
51
52
53
54
|
# File 'lib/rbcli/components/config/backends/env.rb', line 46
def parse_defaults defaults
final_vars = Hash.new
defaults.each do |key, value|
key_parts = key.to_s.split('_').map { |k| k.downcase.to_sym }
key_parts.delete_at(0) if key_parts.first == @prefix.to_s.downcase.to_sym
deep_assign(final_vars, key_parts, value.is_a?(String) ? translate_value(value) : value)
end
final_vars
end
|
#savable? ⇒ Boolean
56
57
58
|
# File 'lib/rbcli/components/config/backends/env.rb', line 56
def savable?
true
end
|
#save(hash, path = []) ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/rbcli/components/config/backends/env.rb', line 32
def save hash, path = []
hash.each do |key, value|
if !value.is_a? Hash
ENV[([@prefix] + path + [key.upcase]).reject(&:nil?).join('_')] = (value.is_a?(Symbol) ? ':' : '') + value.to_s
else
save value, path + [key.upcase]
end
end
end
|
#save_raw(text) ⇒ Object
42
43
44
|
# File 'lib/rbcli/components/config/backends/env.rb', line 42
def save_raw text
true
end
|