1
0

doc.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. module Docs
  2. class Doc
  3. INDEX_FILENAME = 'index.json'
  4. DB_FILENAME = 'db.json'
  5. class << self
  6. attr_accessor :name, :slug, :type, :version, :abstract, :home_url
  7. def inherited(subclass)
  8. subclass.type = type
  9. end
  10. def name
  11. @name || super.try(:demodulize)
  12. end
  13. def slug
  14. @slug || name.try(:downcase)
  15. end
  16. def path
  17. slug
  18. end
  19. def index_path
  20. File.join path, INDEX_FILENAME
  21. end
  22. def db_path
  23. File.join path, DB_FILENAME
  24. end
  25. def as_json
  26. { name: name,
  27. slug: slug,
  28. type: type,
  29. version: version,
  30. index_path: index_path,
  31. db_path: db_path }
  32. end
  33. def store_page(store, id)
  34. store.open(path) do
  35. if page = new.build_page(id) and store_page?(page)
  36. store.write page[:store_path], page[:output]
  37. true
  38. else
  39. false
  40. end
  41. end
  42. end
  43. def store_pages(store)
  44. index = EntryIndex.new
  45. pages = PageDb.new
  46. store.replace(path) do
  47. new.build_pages do |page|
  48. next unless store_page?(page)
  49. store.write page[:store_path], page[:output]
  50. index.add page[:entries]
  51. pages.add page[:path], page[:output]
  52. end
  53. if index.present?
  54. store.write INDEX_FILENAME, index.to_json
  55. store.write DB_FILENAME, pages.to_json
  56. true
  57. else
  58. false
  59. end
  60. end
  61. end
  62. private
  63. def store_page?(page)
  64. page[:entries].present?
  65. end
  66. end
  67. def initialize
  68. raise NotImplementedError, "#{self.class} is an abstract class and cannot be instantiated." if self.class.abstract
  69. end
  70. def build_page(id, &block)
  71. raise NotImplementedError
  72. end
  73. def build_pages(&block)
  74. raise NotImplementedError
  75. end
  76. end
  77. end