manifest.rb 782 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. require 'yajl/json_gem'
  2. module Docs
  3. class Manifest
  4. FILENAME = 'docs.json'
  5. def initialize(store, docs)
  6. @store = store
  7. @docs = docs
  8. end
  9. def store
  10. @store.write FILENAME, to_json
  11. end
  12. def as_json
  13. indexed_docs.map(&:as_json).each do |json|
  14. json[:mtime] = doc_mtime(json)
  15. json[:db_size] = doc_db_size(json)
  16. end
  17. end
  18. def to_json
  19. JSON.generate(as_json)
  20. end
  21. private
  22. def indexed_docs
  23. @docs.select do |doc|
  24. @store.exist?(doc.index_path) && @store.exist?(doc.db_path)
  25. end
  26. end
  27. def doc_mtime(doc)
  28. [@store.mtime(doc[:index_path]).to_i, @store.mtime(doc[:db_path]).to_i].max
  29. end
  30. def doc_db_size(doc)
  31. @store.size(doc[:db_path])
  32. end
  33. end
  34. end