file_store.rb 904 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. require 'fileutils'
  2. require 'find'
  3. module Docs
  4. class FileStore < AbstractStore
  5. private
  6. def read_file(path)
  7. File.read(path)
  8. end
  9. def create_file(path, value)
  10. FileUtils.mkpath File.dirname(path)
  11. if value.is_a? Tempfile
  12. FileUtils.move(value, path)
  13. else
  14. File.write(path, value)
  15. end
  16. end
  17. alias_method :update_file, :create_file
  18. def delete_file(path)
  19. if File.directory?(path)
  20. FileUtils.rmtree(path, secure: true)
  21. else
  22. FileUtils.rm(path)
  23. end
  24. end
  25. def file_exist?(path)
  26. File.exists?(path)
  27. end
  28. def file_mtime(path)
  29. File.mtime(path)
  30. end
  31. def list_files(path)
  32. Find.find path do |file|
  33. next if file == path
  34. Find.prune if File.basename(file)[0] == '.'
  35. yield file
  36. Find.prune unless File.exists?(file)
  37. end
  38. end
  39. end
  40. end