Class: UserConfig::YAMLFile
- Inherits:
-
Object
- Object
- UserConfig::YAMLFile
show all
- Defined in:
- lib/user_config.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(path, default, opts = {}) ⇒ YAMLFile
Returns a new instance of YAMLFile.
191
192
193
194
195
196
197
198
|
# File 'lib/user_config.rb', line 191
def initialize(path, default, opts = {})
@path = path
@default = default
@cache = load_yaml_file
if opts[:merge]
@cache.merge!(@default)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
249
250
251
252
253
254
255
|
# File 'lib/user_config.rb', line 249
def method_missing(method_name, *args, &block)
if Hash.method_defined?(method_name)
to_hash(true).__send__(method_name, *args, &block)
else
super
end
end
|
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
185
186
187
|
# File 'lib/user_config.rb', line 185
def path
@path
end
|
Instance Method Details
#[](key) ⇒ Object
233
234
235
|
# File 'lib/user_config.rb', line 233
def [](key)
@cache[key] || @default[key]
end
|
#[]=(key, val) ⇒ Object
237
238
239
|
# File 'lib/user_config.rb', line 237
def []=(key, val)
@cache[key] = val
end
|
#delete(key) ⇒ Object
241
242
243
|
# File 'lib/user_config.rb', line 241
def delete(key)
@cache.delete(key)
end
|
#save ⇒ Object
Save cached values to the path of file.
224
225
226
227
228
229
230
231
|
# File 'lib/user_config.rb', line 224
def save
unless File.exist?((dir = File.dirname(path)))
FileUtils.mkdir_p(dir)
end
Kernel.open(path, 'w') do |f|
YAML.dump(@cache, f)
end
end
|
#set?(key) ⇒ Boolean
245
246
247
|
# File 'lib/user_config.rb', line 245
def set?(key)
@cache.has_key?(key)
end
|
#to_hash(merge = false) ⇒ Hash
Returns A hash created by merging default values and cached values.
215
216
217
218
219
220
221
|
# File 'lib/user_config.rb', line 215
def to_hash(merge = false)
if merge
@default.merge(@cache)
else
@cache
end
end
|
#to_yaml ⇒ Object
209
210
211
|
# File 'lib/user_config.rb', line 209
def to_yaml
YAML.dump(@cache)
end
|