History

Refactor Doc.store_pages

Thibaut committed 11 years ago

Commit e9125c61 parent ecf774e2 files · +50 −92Browse files

Unified · Split

2 files
  1. lib/docs/core/doc.rb +14 −13
  2. test/lib/docs/core/doc_test.rb +36 −79
lib/docs/core/doc.rb+14−13
@@ -44,21 +44,22 @@ module Docs · 43 unmodified lines
4444 end
4545 end
4646
47− def index_pages
47+ def store_pages(store)
4848 index = EntryIndex.new
49− new.build_pages do |page|
50− next if page[:entries].blank?
51− yield page[:store_path], page[:output]
52− index.add page[:entries]
53− end
54− index.empty? ? nil : index
55− end
5649
57− def store_pages(store)
58− store.replace path do
59− index = index_pages(&store.method(:write))
60− store.write INDEX_FILENAME, index.to_json if index
61− !!index
50+ store.replace(path) do
51+ new.build_pages do |page|
52+ next unless store_page?(page)
53+ store.write page[:store_path], page[:output]
54+ index.add page[:entries]
55+ end
56+
57+ if index.present?
58+ store.write INDEX_FILENAME, index.to_json
59+ true
60+ else
61+ false
62+ end
6263 end
6364 end
6465
test/lib/docs/core/doc_test.rb+36−79
@@ -17,10 +17,6 @@ class DocsDocTest < MiniTest::Spec · 16 unmodified lines
1717 Docs::Entry.new
1818 end
1919
20− let :index do
21− Docs::EntryIndex.new
22− end
23−
2420 let :store do
2521 Docs::NullStore.new
2622 end
@@ -184,18 +180,18 @@ class DocsDocTest < MiniTest::Spec · 157 unmodified lines
184180 end
185181 end
186182
187− describe ".index_pages" do
183+ describe ".store_pages" do
188184 it "build the pages" do
189185 any_instance_of(doc) do |instance|
190186 stub(instance).build_pages { @called = true }
191187 end
192− doc.index_pages {}
188+ doc.store_pages(store) {}
193189 assert @called
194190 end
195191
196192 context "when pages are built successfully" do
197193 let :pages do
198− [page, page.dup]
194+ [page.dup, page.dup]
199195 end
200196
201197 before do
@@ -204,39 +200,51 @@ class DocsDocTest < MiniTest::Spec · 2 unmodified lines
204200 end
205201 end
206202
207− it "yields pages that have :entries" do
208− doc.index_pages { |*args| (@args ||= []) << args }
209− assert_equal pages.length, @args.length
210− assert_equal [page[:store_path], page[:output]], @args.first
211− end
203+ context "and at least one page has :entries" do
204+ it "returns true" do
205+ assert doc.store_pages(store)
206+ end
212207
213− it "doesn't yield pages that don't have :entries" do
214− pages.first[:entries] = []
215− doc.index_pages { |*args| (@args ||= []) << args }
216− assert_equal pages.length - 1, @args.length
217− end
208+ it "stores a file for each page that has :entries" do
209+ pages.first.merge!(entries: [], output: '')
210+ mock(store).write(page[:store_path], page[:output])
211+ mock(store).write('index.json', anything)
212+ doc.store_pages(store)
213+ end
218214
219− describe "and at least one has :entries" do
220− it "returns an EntryIndex" do
221− assert_instance_of Docs::EntryIndex, doc.index_pages {}
215+ it "stores an index that contains all the pages' entries" do
216+ stub(store).write(page[:store_path], page[:output])
217+ mock(store).write('index.json', anything) do |path, json|
218+ json = JSON.parse(json)
219+ assert_equal pages.length, json['entries'].length
220+ assert_includes json['entries'], entry.as_json.stringify_keys
221+ end
222+ doc.store_pages(store)
222223 end
223224
224− describe "the index" do
225− it "contains all the pages' entries" do
226− index = doc.index_pages {}
227− assert_equal pages.length, index.entries.length
228− assert_includes index.entries, entry
225+ it "replaces the .path directory before storing the files" do
226+ stub(doc).path { 'path' }
227+ stub(store).write { assert false }
228+ mock(store).replace('path') do |_, block|
229+ stub(store).write
230+ block.call
229231 end
232+ doc.store_pages(store)
230233 end
231234 end
232235
233− context "and none have :entries" do
236+ context "and no pages have :entries" do
234237 before do
235238 pages.each { |page| page[:entries] = [] }
236239 end
237240
238− it "returns nil" do
239− assert_nil doc.index_pages {}
241+ it "returns false" do
242+ refute doc.store_pages(store)
243+ end
244+
245+ it "doesn't store files" do
246+ dont_allow(store).write
247+ doc.store_pages(store)
240248 end
241249 end
242250 end
@@ -248,57 +256,6 @@ class DocsDocTest < MiniTest::Spec · 5 unmodified lines
248256 end
249257 end
250258
251− it "doesn't yield" do
252− doc.index_pages { |*_| @yield = true }
253− refute @yield
254− end
255−
256− it "returns nil" do
257− assert_nil doc.index_pages {}
258− end
259− end
260− end
261−
262− describe ".store_pages" do
263− context "when pages are indexed successfully" do
264− before do
265− stub(store).write
266− stub(doc).index_pages do |block|
267− 2.times { block.call page[:store_path], page[:output] }
268− index
269− end
270− end
271−
272− it "returns true" do
273− assert doc.store_pages(store)
274− end
275−
276− it "stores a file for each page" do
277− 2.times { mock(store).write(page[:store_path], page[:output]) }
278− doc.store_pages(store)
279− end
280−
281− it "stores the index" do
282− mock(store).write('index.json', index.to_json)
283− doc.store_pages(store)
284− end
285−
286− it "replaces the .path directory before storing the files" do
287− stub(doc).path { 'path' }
288− stub(store).write { assert false }
289− mock(store).replace('path') do |_, block|
290− stub(store).write
291− block.call
292− end
293− doc.store_pages(store)
294− end
295− end
296−
297− context "when no pages are indexed successfully" do
298− before do
299− stub(doc).index_pages { nil }
300− end
301−
302259 it "returns false" do
303260 refute doc.store_pages(store)
304261 end