# Return true if the file is writable, otherwise false.
#
def is_writable_file?(file)
+ if File.exist?(file)
+ if FileTest.file?(file)
+ return FileTest.writable_real?(file)
+ else
+ return false
+ end
+ end
+
begin
open(file, "w") {|fh| }
+ FileUtils.rm file
rescue
return false
end
- unless FileTest.file? file
- return false
- end
return true
end
def load
return unless @file.exist?
- yaml = @file.open("r") {|f| f.read}
- @records = YAML.load(yaml)
+ @records = YAML.load_file(@file)
+ unless @records && @records.instance_of?(Array)
+ $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
+ @records = [].to_yaml
+ end
end
def save
begin
- @file.open("w+") do |f|
+ @file.open("w") do |f|
f << YAML.dump(@records)
end
rescue Errno::ENOSPC