From 5c46eabc67260376385f59760ce8c81a946357af Mon Sep 17 00:00:00 2001 From: Thibaut Date: Wed, 31 Dec 2014 13:54:29 -0500 Subject: [PATCH] Output a JSON file containing all the pages' content --- lib/docs/core/doc.rb | 4 ++++ lib/docs/core/page_db.rb | 25 +++++++++++++++++++++++++ test/lib/docs/core/doc_test.rb | 14 +++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/docs/core/page_db.rb diff --git a/lib/docs/core/doc.rb b/lib/docs/core/doc.rb index 9412648f..c0772a1c 100644 --- a/lib/docs/core/doc.rb +++ b/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 diff --git a/lib/docs/core/page_db.rb b/lib/docs/core/page_db.rb new file mode 100644 index 00000000..1ce1e622 --- /dev/null +++ b/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 diff --git a/test/lib/docs/core/doc_test.rb b/test/lib/docs/core/doc_test.rb index a1b4475e..9cf65270 100644 --- a/test/lib/docs/core/doc_test.rb +++ b/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 }