Class: Confg::Configuration
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Confg::Configuration
show all
- Defined in:
- lib/confg/configuration.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(env: Confg.env, root: Confg.root) ⇒ Configuration
Returns a new instance of Configuration.
10
11
12
13
14
15
|
# 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)
super({})
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/confg/configuration.rb', line 98
def method_missing(method_name, *args, &block)
key = method_name.to_s
if __getobj__.respond_to?(key)
super
elsif key.end_with?("=") && !args.empty?
set(key[0...-1], args[0])
elsif block_given?
open_block(key, &block)
else
fetch(key)
end
end
|
Instance Attribute Details
#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
47
48
49
|
# File 'lib/confg/configuration.rb', line 47
def fetch(key, &block)
__getobj__.fetch(key.to_s, &block)
end
|
#get(key) ⇒ Object
Also known as:
[]
42
43
44
|
# File 'lib/confg/configuration.rb', line 42
def get(key)
fetch(key) { nil }
end
|
#inspect ⇒ Object
17
18
19
|
# File 'lib/confg/configuration.rb', line 17
def inspect
"#<#{self.class.name} #{__getobj__.inspect}>"
end
|
#load_key(key) ⇒ Object
65
66
67
68
|
# File 'lib/confg/configuration.rb', line 65
def load_key(key)
load_yaml(key, key: key)
end
|
#load_yaml(path, key: nil, ignore_env: false) ⇒ Object
Also known as:
load_yml
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/confg/configuration.rb', line 70
def load_yaml(path, 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.send :load, erb_content
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!
29
30
31
32
33
|
# File 'lib/confg/configuration.rb', line 29
def merge(other)
other.each_pair do |k, v|
set(k, v)
end
end
|
#respond_to_missing?(*_args) ⇒ Boolean
112
113
114
|
# File 'lib/confg/configuration.rb', line 112
def respond_to_missing?(*_args)
true
end
|
#set(key, value = nil) ⇒ Object
Also known as:
[]=
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/confg/configuration.rb', line 51
def set(key, value = nil)
__getobj__[key.to_s] = case value
when ::Hash
open_block(key) do |child|
value.each_pair do |k, v|
child.set(k, v)
end
end
else
value
end
end
|
#tmp(key, value) ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/confg/configuration.rb', line 21
def tmp(key, value)
initial = get(key)
set(key, value)
yield
ensure
set(key, initial)
end
|
#to_h ⇒ Object
36
37
38
39
40
|
# File 'lib/confg/configuration.rb', line 36
def to_h
__getobj__.transform_values do |v|
v.is_a?(self.class) ? v.to_h : v
end
end
|