lib/vfs/entries/file.rb in vfs-0.3.13 vs lib/vfs/entries/file.rb in vfs-0.3.14

- old
+ new

@@ -51,55 +51,72 @@ options[:override] = true create options end def write *args, &block + if block + options = args.first || {} + else + data, options = *args + options ||= {} + end + raise "can't do :override and :append at the same time!" if options[:override] and options[:append] + storage.open_fs do |fs| + # TODO2 Performance lost, extra call to check file existence + # We need to check if the file exist before writing to it, otherwise it's + # impossible to distinguish if the StandardError caused by the 'already exist' error or + # some other error. + entry = self.entry + if entry.exist? + if options[:override] + entry.destroy + else + raise Error, "entry #{self} already exist!" + end + end + try = 0 begin try += 1 if block - options = args.first || {} - else - data, options = *args - options ||= {} - end - raise "can't do :override and :append at the same time!" if options[:override] and options[:append] - if block fs.write_file(path, options[:append], &block) else fs.write_file(path, options[:append]){|writer| writer.write data} end rescue StandardError => error - entry = self.entry - if entry.exist? - if options[:override] - entry.destroy - else - raise Error, "entry #{self} already exist!" - end + parent = self.parent + if parent.exist? + # some unknown error + raise error else - parent = self.parent - if parent.exist? - # some unknown error - raise error - else - parent.create(options) - end + parent.create(options) end - retry if try < 2 + try < 2 ? retry : raise(error) end end self end def write! *args, &block args << {} unless args.last.is_a? Hash args.last[:override] = true write *args, &block end + def append *args, &block + options = (args.last.is_a?(Hash) && args.pop) || {} + options[:append] = true + write(*(args << options), &block) + end + + def update options = {}, &block + options[:override] = true + data = read options + write block.call(data), options + end + def destroy options = {} storage.open_fs do |fs| begin fs.delete_file path self @@ -122,27 +139,9 @@ self end def destroy! options = {} options[:force] = true destroy options - end - - def append *args, &block - if block - options = args.first || {} - else - data, options = *args - options ||= {} - end - - options[:append] = true - write data, options, &block - end - - def update options = {}, &block - options[:override] = true - data = read options - write block.call(data), options end # # Transfers \ No newline at end of file