lib/vfs/entries/file.rb in vfs-0.3.15 vs lib/vfs/entries/file.rb in vfs-0.4.0

- old
+ new

@@ -9,17 +9,17 @@ # # CRUD # def read options = {}, &block options[:bang] = true unless options.include? :bang - storage.open do |fs| + driver.open do begin if block - fs.read_file path, &block + driver.read_file path, &block else data = "" - fs.read_file(path){|buff| data << buff} + driver.read_file(path){|buff| data << buff} data end rescue StandardError => e raise Vfs::Error, "can't read Dir #{self}!" if dir.exist? attrs = get @@ -37,113 +37,68 @@ end end end end - def content options = {} - read options - end + # def content options = {} + # read options + # end def create options = {} write '', options self end - def create! options = {} - 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 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 - + driver.open do try = 0 begin try += 1 if block - fs.write_file(path, options[:append], &block) + driver.write_file(path, options[:append], &block) else - fs.write_file(path, options[:append]){|writer| writer.write data} + driver.write_file(path, options[:append]){|writer| writer.write data} end rescue StandardError => error parent = self.parent - if parent.exist? - # some unknown error - raise error - else + if entry.exist? + entry.destroy + elsif !parent.exist? parent.create(options) + else + # unknown error + raise error end 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 do |fs| - begin - fs.delete_file path - self - rescue StandardError => e - attrs = get - if attrs and attrs[:dir] - if options[:force] - dir.destroy - else - raise Error, "can't destroy Dir #{dir} (you are trying to destroy it as if it's a File)" - end - elsif attrs and attrs[:file] - # unknown internal error - raise e - else - # do nothing, file already not exist - end - end - end - self + def destroy + destroy_entry end - def destroy! options = {} - options[:force] = true - destroy options - end # # Transfers # @@ -164,22 +119,14 @@ read(options){|buff| writer.write buff} end target end - def copy_to! to, options = {} - options[:override] = true - copy_to to, options - end - def move_to to, options = {} - copy_to to, options - destroy options + def move_to to + copy_to to + destroy to - end - def move_to! to, options = {} - options[:override] = true - move_to to, options end # # Extra Stuff \ No newline at end of file