Pārlūkot izejas kodu

Output a JSON file containing all the pages' content

Thibaut 11 gadi atpakaļ
vecāks
revīzija
5c46eabc67
3 mainītis faili ar 42 papildinājumiem un 1 dzēšanām
  1. 4 0
      lib/docs/core/doc.rb
  2. 25 0
      lib/docs/core/page_db.rb
  3. 13 1
      test/lib/docs/core/doc_test.rb

+ 4 - 0
lib/docs/core/doc.rb

@@ -1,6 +1,7 @@
 module Docs
   class Doc
     INDEX_FILENAME = 'index.json'
+    DB_FILENAME = 'db.json'
 
     class << self
       attr_accessor :name, :slug, :type, :version, :abstract
@@ -46,16 +47,19 @@ module Docs
 
       def store_pages(store)
         index = EntryIndex.new
+        pages = PageDb.new
 
         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

+ 25 - 0
lib/docs/core/page_db.rb

@@ -0,0 +1,25 @@
+require 'yajl/json_gem'
+
+module Docs
+  class PageDb
+    attr_reader :pages
+
+    delegate :empty?, :blank?, to: :pages
+
+    def initialize
+      @pages = {}
+    end
+
+    def add(path, content)
+      @pages[path] = content
+    end
+
+    def as_json
+      @pages
+    end
+
+    def to_json
+      JSON.generate(as_json)
+    end
+  end
+end

+ 13 - 1
test/lib/docs/core/doc_test.rb

@@ -10,7 +10,7 @@ class DocsDocTest < MiniTest::Spec
   end
 
   let :page do
-    { store_path: 'store_path', output: 'output', entries: [entry] }
+    { path: 'path', store_path: 'store_path', output: 'output', entries: [entry] }
   end
 
   let :entry do
@@ -209,11 +209,13 @@ class DocsDocTest < MiniTest::Spec
           pages.first.merge!(entries: [], output: '')
           mock(store).write(page[:store_path], page[:output])
           mock(store).write('index.json', anything)
+          mock(store).write('db.json', anything)
           doc.store_pages(store)
         end
 
         it "stores an index that contains all the pages' entries" do
           stub(store).write(page[:store_path], page[:output])
+          stub(store).write('db.json', anything)
           mock(store).write('index.json', anything) do |path, json|
             json = JSON.parse(json)
             assert_equal pages.length, json['entries'].length
@@ -222,6 +224,16 @@ class DocsDocTest < MiniTest::Spec
           doc.store_pages(store)
         end
 
+        it "stores a db that contains all the pages, indexed by path" do
+          stub(store).write(page[:store_path], page[:output])
+          stub(store).write('index.json', anything)
+          mock(store).write('db.json', anything) do |path, json|
+            json = JSON.parse(json)
+            assert_equal page[:output], json[page[:path]]
+          end
+          doc.store_pages(store)
+        end
+
         it "replaces the .path directory before storing the files" do
           stub(doc).path { 'path' }
           stub(store).write { assert false }