1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 41 42 43 44 45 46 47
|
require "yaml"
require "librarian/config/source"
module Librarian
module Config
class FileSource < Source
attr_accessor :config_path
private :config_path=
def initialize(adapter_name, options = { })
super
self.config_path = options.delete(:config_path) or raise ArgumentError, "must provide config_path"
end
def to_s
config_path
end
private
def load
return { } unless File.file?(config_path)
raw = YAML.load_file(config_path)
return { } unless Hash === raw
translate_raw_to_config(raw)
end
def save(config)
raw = translate_config_to_raw(config)
if config.empty?
File.delete(config_path) if File.file?(config_path)
else
config_dir = File.dirname(config_path)
FileUtils.mkpath(config_dir) unless File.directory?(config_dir)
File.open(config_path, "wb"){|f| YAML.dump(raw, f)}
end
end
end
end
end
|