Refactor Doc.store_page

pull/165/head
Thibaut 10 years ago
parent 84eb727f5d
commit 1655a00fb6

@ -33,12 +33,14 @@ module Docs
index_path: index_path } index_path: index_path }
end end
def index_page(id) def store_page(store, id)
if (page = new.build_page(id)) && page[:entries].present? store.open(path) do
yield page[:store_path], page[:output] if page = new.build_page(id) and store_page?(page)
index = EntryIndex.new store.write page[:store_path], page[:output]
index.add page[:entries] true
index else
false
end
end end
end end
@ -52,13 +54,6 @@ module Docs
index.empty? ? nil : index index.empty? ? nil : index
end end
def store_page(store, id)
store.open path do
index = index_page(id, &store.method(:write))
!!index
end
end
def store_pages(store) def store_pages(store)
store.replace path do store.replace path do
index = index_pages(&store.method(:write)) index = index_pages(&store.method(:write))
@ -66,6 +61,12 @@ module Docs
!!index !!index
end end
end end
private
def store_page?(page)
page[:entries].present?
end
end end
def initialize def initialize

@ -113,12 +113,12 @@ class DocsDocTest < MiniTest::Spec
end end
end end
describe ".index_page" do describe ".store_page" do
it "builds a page" do it "builds a page" do
any_instance_of(doc) do |instance| any_instance_of(doc) do |instance|
stub(instance).build_page('id') { @called = true; nil } stub(instance).build_page('id') { @called = true; nil }
end end
doc.index_page('id') {} doc.store_page(store, 'id') {}
assert @called assert @called
end end
@ -130,20 +130,23 @@ class DocsDocTest < MiniTest::Spec
end end
context "and it has :entries" do context "and it has :entries" do
it "yields the page's :store_path and :output" do it "returns true" do
doc.index_page('') { |*args| @args = args } assert doc.store_page(store, 'id')
assert_equal [page[:store_path], page[:output]], @args
end end
it "returns an EntryIndex" do it "stores a file" do
assert_instance_of Docs::EntryIndex, doc.index_page('') {} mock(store).write(page[:store_path], page[:output])
doc.store_page(store, 'id')
end end
describe "the index" do it "opens the .path directory before storing the file" do
it "contains the page's entries" do stub(doc).path { 'path' }
index = doc.index_page('') {} stub(store).write { assert false }
assert_equal page[:entries], index.entries mock(store).open('path') do |_, block|
stub(store).write
block.call
end end
doc.store_page(store, 'id')
end end
end end
@ -152,13 +155,13 @@ class DocsDocTest < MiniTest::Spec
page[:entries] = [] page[:entries] = []
end end
it "doesn't yield" do it "returns false" do
doc.index_page('') { |*_| @yield = true } refute doc.store_page(store, 'id')
refute @yield
end end
it "returns nil" do it "doesn't store a file" do
assert_nil doc.index_page('') {} dont_allow(store).write
doc.store_page(store, 'id')
end end
end end
end end
@ -170,13 +173,13 @@ class DocsDocTest < MiniTest::Spec
end end
end end
it "doesn't yield" do it "returns false" do
doc.index_page('') { |*_| @yield = true } refute doc.store_page(store, 'id')
refute @yield
end end
it "returns nil" do it "doesn't store a file" do
assert_nil doc.index_page('') {} dont_allow(store).write
doc.store_page(store, 'id')
end end
end end
end end
@ -256,48 +259,6 @@ class DocsDocTest < MiniTest::Spec
end end
end end
describe ".store_page" do
context "when the page is indexed successfully" do
before do
stub(doc).index_page('id').yields(page[:store_path], page[:output]) { index }
end
it "returns true" do
assert doc.store_page(store, 'id')
end
it "stores a file" do
mock(store).write(page[:store_path], page[:output])
doc.store_page(store, 'id')
end
it "opens the .path directory before storing the file" do
stub(doc).path { 'path' }
stub(store).write { assert false }
mock(store).open('path') do |_, block|
stub(store).write
block.call
end
doc.store_page(store, 'id')
end
end
context "when the page isn't indexed successfully" do
before do
stub(doc).index_page('id') { nil }
end
it "returns false" do
refute doc.store_page(store, 'id')
end
it "doesn't store a file" do
dont_allow(store).write
doc.store_page(store, 'id')
end
end
end
describe ".store_pages" do describe ".store_pages" do
context "when pages are indexed successfully" do context "when pages are indexed successfully" do
before do before do

Loading…
Cancel
Save