1
0

manifest.rb 797 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 do |doc|
  14. json = doc.as_json
  15. json[:mtime] = doc_mtime(doc)
  16. json[:db_size] = doc_db_size(doc)
  17. json
  18. end
  19. end
  20. def to_json
  21. JSON.generate(as_json)
  22. end
  23. private
  24. def indexed_docs
  25. @docs.select do |doc|
  26. @store.exist?(doc.index_path) && @store.exist?(doc.db_path)
  27. end
  28. end
  29. def doc_mtime(doc)
  30. [@store.mtime(doc.index_path).to_i, @store.mtime(doc.db_path).to_i].max
  31. end
  32. def doc_db_size(doc)
  33. @store.size(doc.db_path)
  34. end
  35. end
  36. end