Refactor Doc.store_pages

pull/165/head
Thibaut 10 years ago
parent ecf774e22c
commit e9125c6ec2

@ -44,21 +44,22 @@ module Docs
end end
end end
def index_pages def store_pages(store)
index = EntryIndex.new index = EntryIndex.new
new.build_pages do |page|
next if page[:entries].blank?
yield page[:store_path], page[:output]
index.add page[:entries]
end
index.empty? ? nil : index
end
def store_pages(store) store.replace(path) do
store.replace path do new.build_pages do |page|
index = index_pages(&store.method(:write)) next unless store_page?(page)
store.write INDEX_FILENAME, index.to_json if index store.write page[:store_path], page[:output]
!!index index.add page[:entries]
end
if index.present?
store.write INDEX_FILENAME, index.to_json
true
else
false
end
end end
end end

@ -17,10 +17,6 @@ class DocsDocTest < MiniTest::Spec
Docs::Entry.new Docs::Entry.new
end end
let :index do
Docs::EntryIndex.new
end
let :store do let :store do
Docs::NullStore.new Docs::NullStore.new
end end
@ -184,18 +180,18 @@ class DocsDocTest < MiniTest::Spec
end end
end end
describe ".index_pages" do describe ".store_pages" do
it "build the pages" do it "build the pages" do
any_instance_of(doc) do |instance| any_instance_of(doc) do |instance|
stub(instance).build_pages { @called = true } stub(instance).build_pages { @called = true }
end end
doc.index_pages {} doc.store_pages(store) {}
assert @called assert @called
end end
context "when pages are built successfully" do context "when pages are built successfully" do
let :pages do let :pages do
[page, page.dup] [page.dup, page.dup]
end end
before do before do
@ -204,39 +200,51 @@ class DocsDocTest < MiniTest::Spec
end end
end end
it "yields pages that have :entries" do context "and at least one page has :entries" do
doc.index_pages { |*args| (@args ||= []) << args } it "returns true" do
assert_equal pages.length, @args.length assert doc.store_pages(store)
assert_equal [page[:store_path], page[:output]], @args.first end
end
it "doesn't yield pages that don't have :entries" do it "stores a file for each page that has :entries" do
pages.first[:entries] = [] pages.first.merge!(entries: [], output: '')
doc.index_pages { |*args| (@args ||= []) << args } mock(store).write(page[:store_path], page[:output])
assert_equal pages.length - 1, @args.length mock(store).write('index.json', anything)
end doc.store_pages(store)
end
describe "and at least one has :entries" do it "stores an index that contains all the pages' entries" do
it "returns an EntryIndex" do stub(store).write(page[:store_path], page[:output])
assert_instance_of Docs::EntryIndex, doc.index_pages {} mock(store).write('index.json', anything) do |path, json|
json = JSON.parse(json)
assert_equal pages.length, json['entries'].length
assert_includes json['entries'], entry.as_json.stringify_keys
end
doc.store_pages(store)
end end
describe "the index" do it "replaces the .path directory before storing the files" do
it "contains all the pages' entries" do stub(doc).path { 'path' }
index = doc.index_pages {} stub(store).write { assert false }
assert_equal pages.length, index.entries.length mock(store).replace('path') do |_, block|
assert_includes index.entries, entry stub(store).write
block.call
end end
doc.store_pages(store)
end end
end end
context "and none have :entries" do context "and no pages have :entries" do
before do before do
pages.each { |page| page[:entries] = [] } pages.each { |page| page[:entries] = [] }
end end
it "returns nil" do it "returns false" do
assert_nil doc.index_pages {} refute doc.store_pages(store)
end
it "doesn't store files" do
dont_allow(store).write
doc.store_pages(store)
end end
end end
end end
@ -248,57 +256,6 @@ class DocsDocTest < MiniTest::Spec
end end
end end
it "doesn't yield" do
doc.index_pages { |*_| @yield = true }
refute @yield
end
it "returns nil" do
assert_nil doc.index_pages {}
end
end
end
describe ".store_pages" do
context "when pages are indexed successfully" do
before do
stub(store).write
stub(doc).index_pages do |block|
2.times { block.call page[:store_path], page[:output] }
index
end
end
it "returns true" do
assert doc.store_pages(store)
end
it "stores a file for each page" do
2.times { mock(store).write(page[:store_path], page[:output]) }
doc.store_pages(store)
end
it "stores the index" do
mock(store).write('index.json', index.to_json)
doc.store_pages(store)
end
it "replaces the .path directory before storing the files" do
stub(doc).path { 'path' }
stub(store).write { assert false }
mock(store).replace('path') do |_, block|
stub(store).write
block.call
end
doc.store_pages(store)
end
end
context "when no pages are indexed successfully" do
before do
stub(doc).index_pages { nil }
end
it "returns false" do it "returns false" do
refute doc.store_pages(store) refute doc.store_pages(store)
end end

Loading…
Cancel
Save