Class: TivoHMO::Config
- Inherits:
-
Object
show all
- Includes:
- GemLogger::LoggerSupport, Singleton
- Defined in:
- lib/tivohmo/config.rb
Defined Under Namespace
Modules: Mixin
Classes: Data
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
9
10
11
12
13
|
# File 'lib/tivohmo/config.rb', line 9
def initialize
super
@primary_data = Data.new
@secondary_data = Data.new
end
|
Instance Method Details
#get(scoped_key) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/tivohmo/config.rb', line 52
def get(scoped_key)
scoped_key = Array(scoped_key)
result = nil
key = scoped_key.pop
path = scoped_key
(0..path.size).to_a.reverse.each do |i|
partial = path[0, i] << key
begin
result = @secondary_data.deep_fetch(*partial)
rescue Data::UndefinedPathError
begin
result = @primary_data.deep_fetch(*partial)
rescue Data::UndefinedPathError
end
end
break if ! result.nil?
end
if result.nil?
registered = known_config[key]
result = registered[:default_value] if registered
end
result
end
|
#known_config ⇒ Object
48
49
50
|
# File 'lib/tivohmo/config.rb', line 48
def known_config
@known_config ||= {}
end
|
#reset ⇒ Object
42
43
44
45
46
|
# File 'lib/tivohmo/config.rb', line 42
def reset
@primary_file = @secondary_file = nil
@primary_data = @secondary_data = nil
@known_config = nil
end
|
#set(scoped_key, value, persist = true) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/tivohmo/config.rb', line 81
def set(scoped_key, value, persist=true)
scoped_key = Array(scoped_key)
key = scoped_key.pop
val_hash = {key => value}
scoped_key.reverse.each do |k|
val_hash = {k => val_hash}
end
@secondary_data = @secondary_data.deep_merge(val_hash)
File.write(@secondary_file, YAML.dump(@secondary_data)) if persist && @secondary_file
registered = known_config[key]
if registered && registered[:on_change]
registered[:on_change].call
end
end
|
#setup(primary_filename, secondary_filename = nil) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/tivohmo/config.rb', line 16
def setup(primary_filename, secondary_filename=nil)
@primary_file = primary_filename
@secondary_file = secondary_filename
@primary_data = Data.new
@secondary_data = Data.new
if File.exist?(@primary_file.to_s)
logger.info "Loading primary config from: '#{@primary_file}'"
@primary_data = Data.load(@primary_file)
else
logger.info "No primary config at file: '#{@primary_file}'"
end
secondary = get(:settings)
@secondary_file ||= File.expand_path(secondary) if secondary
if File.exist?(@secondary_file.to_s)
logger.info "Loading secondary config from: '#{@secondary_file}'"
@secondary_data = Data.load(@secondary_file)
else
logger.info "No secondary config at file: '#{@secondary_file}'"
end
end
|