You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/lib/docs/core/doc.rb

95 lines
1.9 KiB

11 years ago
module Docs
class Doc
INDEX_FILENAME = 'index.json'
DB_FILENAME = 'db.json'
11 years ago
class << self
attr_accessor :name, :slug, :type, :version, :abstract
def inherited(subclass)
subclass.type = type
end
def name
@name || super.try(:demodulize)
end
def slug
@slug || name.try(:downcase)
end
def path
slug
end
def index_path
File.join path, INDEX_FILENAME
end
10 years ago
def db_path
File.join path, DB_FILENAME
end
11 years ago
def as_json
{ name: name,
slug: slug,
type: type,
version: version,
10 years ago
index_path: index_path,
db_path: db_path }
11 years ago
end
def store_page(store, id)
store.open(path) do
if page = new.build_page(id) and store_page?(page)
store.write page[:store_path], page[:output]
true
else
false
end
11 years ago
end
end
def store_pages(store)
11 years ago
index = EntryIndex.new
pages = PageDb.new
11 years ago
store.replace(path) do
new.build_pages do |page|
next unless store_page?(page)
store.write page[:store_path], page[:output]
index.add page[:entries]
pages.add page[:path], page[:output]
end
if index.present?
store.write INDEX_FILENAME, index.to_json
store.write DB_FILENAME, pages.to_json
true
else
false
end
11 years ago
end
end
private
def store_page?(page)
page[:entries].present?
end
11 years ago
end
def initialize
raise NotImplementedError, "#{self.class} is an abstract class and cannot be instantiated." if self.class.abstract
end
def build_page(id, &block)
raise NotImplementedError
end
def build_pages(&block)
raise NotImplementedError
end
end
end