Class: FlexConfig
- Inherits:
-
Object
show all
- Defined in:
- lib/flex_config.rb,
lib/flex_config/config_obj.rb
Defined Under Namespace
Classes: ConfigObj
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(path, configs) ⇒ FlexConfig
Returns a new instance of FlexConfig.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/flex_config.rb', line 16
def initialize (path, configs)
file_path = check_path(path) + '/config.yml'
config = check_file(file_path)
case configs
when Hash
config.merge!(configs) unless configs == {}
when Array
new = {}
configs.each { |key| new[key] = {} }
config.merge!(new)
else
raise ArgumentError('Second argument must be a hash or array of keys')
end
@config = config
@file_path = file_path
save_config
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
38
39
40
|
# File 'lib/flex_config.rb', line 38
def method_missing (method, *args)
@config.send(method, *args)
end
|
Class Method Details
.get_config(path) ⇒ Object
9
10
11
12
|
# File 'lib/flex_config.rb', line 9
def get_config (path)
file_path = path + '/config.yml'
YAML::load(File.open(file_path, 'r'))
end
|
Instance Method Details
#[](key) ⇒ Object
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/flex_config.rb', line 42
def [] (key)
state =
if @config[key].nil?
{}
else
@config[key].deep_copy
end
ConfigObj.new(self, key, state)
end
|
#check_file(path) ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/flex_config.rb', line 65
def check_file (path)
if File.exist?(path)
YAML::load(File.open(path, 'r'))
else
{}
end
end
|
#check_path(path) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/flex_config.rb', line 57
def check_path (path)
dir_path = path + '/config'
unless Dir.exist?(dir_path)
Dir.mkdir(dir_path)
end
dir_path
end
|
#save_config ⇒ Object
73
74
75
76
77
78
|
# File 'lib/flex_config.rb', line 73
def save_config
output = File.open(@file_path, 'w')
output.puts @config.to_yaml
output.close
@config
end
|
#to_h ⇒ Object
53
54
55
|
# File 'lib/flex_config.rb', line 53
def to_h
@config
end
|