abstract_store.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. require 'pathname'
  2. module Docs
  3. class AbstractStore
  4. class InvalidPathError < StandardError; end
  5. class LockError < StandardError; end
  6. include Instrumentable
  7. def initialize(path)
  8. path = Pathname.new(path).cleanpath
  9. raise ArgumentError if path.relative?
  10. @root_path = @working_path = path.freeze
  11. end
  12. def root_path
  13. @root_path.to_s
  14. end
  15. def working_path
  16. @working_path.to_s
  17. end
  18. def expand_path(path)
  19. join_paths @working_path, path
  20. end
  21. def open(path, &block)
  22. if block_given?
  23. open_yield_close(path, &block)
  24. else
  25. set_working_path join_paths(@root_path, path)
  26. end
  27. end
  28. def close
  29. set_working_path @root_path
  30. end
  31. def read(path)
  32. path = expand_path(path)
  33. read_file(path) if file_exist?(path)
  34. end
  35. def write(path, value)
  36. path = expand_path(path)
  37. touch(path)
  38. if file_exist?(path)
  39. update(path, value)
  40. else
  41. create(path, value)
  42. end
  43. end
  44. def delete(path)
  45. path = expand_path(path)
  46. if file_exist?(path)
  47. destroy(path)
  48. true
  49. end
  50. end
  51. def exist?(path)
  52. file_exist? expand_path(path)
  53. end
  54. def mtime(path)
  55. path = expand_path(path)
  56. file_mtime(path) if file_exist?(path)
  57. end
  58. def each(&block)
  59. list_files(working_path, &block)
  60. end
  61. def replace(path = nil, &block)
  62. if path
  63. return open(path) { replace(&block) }
  64. else
  65. lock { track_touched { yield.tap { delete_untouched } } }
  66. end
  67. end
  68. private
  69. def read_file(path)
  70. raise NotImplementedError
  71. end
  72. def create_file(path, value)
  73. raise NotImplementedError
  74. end
  75. def update_file(path, value)
  76. raise NotImplementedError
  77. end
  78. def delete_file(path)
  79. raise NotImplementedError
  80. end
  81. def file_exist?(path)
  82. raise NotImplementedError
  83. end
  84. def file_mtime(path)
  85. raise NotImplementedError
  86. end
  87. def list_files(path, &block)
  88. raise NotImplementedError
  89. end
  90. def set_working_path(path)
  91. @working_path = Pathname.new(path).freeze if assert_unlocked
  92. end
  93. def join_paths(base, path)
  94. base = Pathname.new(base).cleanpath
  95. path = Pathname.new(path).cleanpath
  96. path = base + path unless path.absolute?
  97. unless File.join(path, '').start_with? File.join(base, '')
  98. raise InvalidPathError, "Tried accessing #{path} outside #{base}"
  99. end
  100. path.to_s
  101. end
  102. def open_yield_close(path)
  103. working_path_was = working_path
  104. open(path)
  105. yield
  106. ensure
  107. set_working_path working_path_was
  108. end
  109. def create(path, value)
  110. instrument 'create.store', path: path do
  111. create_file(path, value)
  112. end
  113. end
  114. def update(path, value)
  115. instrument 'update.store', path: path do
  116. update_file(path, value)
  117. end
  118. end
  119. def destroy(path)
  120. instrument 'destroy.store', path: path do
  121. delete_file(path)
  122. end
  123. end
  124. def lock
  125. assert_unlocked
  126. @locked = true
  127. yield
  128. ensure
  129. @locked = false
  130. end
  131. def assert_unlocked
  132. raise LockError if @locked
  133. true
  134. end
  135. def track_touched
  136. @touched = []
  137. yield
  138. ensure
  139. @touched = nil
  140. end
  141. def touch(path)
  142. @touched << path if @touched
  143. end
  144. def touched?(path)
  145. dir = File.join(path, '')
  146. @touched.any? do |touched_path|
  147. touched_path == path || touched_path.start_with?(dir)
  148. end
  149. end
  150. def delete_untouched
  151. return if @touched.empty?
  152. each do |path|
  153. destroy(path) unless touched?(path)
  154. end
  155. end
  156. end
  157. end