History

Refactor Doc.store_page

Thibaut committed 11 years ago

Commit 1655a001 parent 84eb7272 files · +38 −76Browse files

Unified · Split

2 files
  1. lib/docs/core/doc.rb +14 −13
  2. test/lib/docs/core/doc_test.rb +24 −63
lib/docs/core/doc.rb+14−13
@@ -33,12 +33,14 @@ module Docs · 32 unmodified lines
3333 index_path: index_path }
3434 end
3535
36− def index_page(id)
37− if (page = new.build_page(id)) && page[:entries].present?
38− yield page[:store_path], page[:output]
39− index = EntryIndex.new
40− index.add page[:entries]
41− index
36+ def store_page(store, id)
37+ store.open(path) do
38+ if page = new.build_page(id) and store_page?(page)
39+ store.write page[:store_path], page[:output]
40+ true
41+ else
42+ false
43+ end
4244 end
4345 end
4446
@@ -52,13 +54,6 @@ module Docs · 7 unmodified lines
5254 index.empty? ? nil : index
5355 end
5456
55− def store_page(store, id)
56− store.open path do
57− index = index_page(id, &store.method(:write))
58− !!index
59− end
60− end
61−
6257 def store_pages(store)
6358 store.replace path do
6459 index = index_pages(&store.method(:write))
@@ -66,6 +61,12 @@ module Docs · 1 unmodified line
6661 !!index
6762 end
6863 end
64+
65+ private
66+
67+ def store_page?(page)
68+ page[:entries].present?
69+ end
6970 end
7071
7172 def initialize
test/lib/docs/core/doc_test.rb+24−63
@@ -113,12 +113,12 @@ class DocsDocTest < MiniTest::Spec · 112 unmodified lines
113113 end
114114 end
115115
116− describe ".index_page" do
116+ describe ".store_page" do
117117 it "builds a page" do
118118 any_instance_of(doc) do |instance|
119119 stub(instance).build_page('id') { @called = true; nil }
120120 end
121− doc.index_page('id') {}
121+ doc.store_page(store, 'id') {}
122122 assert @called
123123 end
124124
@@ -130,20 +130,23 @@ class DocsDocTest < MiniTest::Spec · 5 unmodified lines
130130 end
131131
132132 context "and it has :entries" do
133− it "yields the page's :store_path and :output" do
134− doc.index_page('') { |*args| @args = args }
135− assert_equal [page[:store_path], page[:output]], @args
133+ it "returns true" do
134+ assert doc.store_page(store, 'id')
136135 end
137136
138− it "returns an EntryIndex" do
139− assert_instance_of Docs::EntryIndex, doc.index_page('') {}
137+ it "stores a file" do
138+ mock(store).write(page[:store_path], page[:output])
139+ doc.store_page(store, 'id')
140140 end
141141
142− describe "the index" do
143− it "contains the page's entries" do
144− index = doc.index_page('') {}
145− assert_equal page[:entries], index.entries
142+ it "opens the .path directory before storing the file" do
143+ stub(doc).path { 'path' }
144+ stub(store).write { assert false }
145+ mock(store).open('path') do |_, block|
146+ stub(store).write
147+ block.call
146148 end
149+ doc.store_page(store, 'id')
147150 end
148151 end
149152
@@ -152,13 +155,13 @@ class DocsDocTest < MiniTest::Spec · 2 unmodified lines
152155 page[:entries] = []
153156 end
154157
155− it "doesn't yield" do
156− doc.index_page('') { |*_| @yield = true }
157− refute @yield
158+ it "returns false" do
159+ refute doc.store_page(store, 'id')
158160 end
159161
160− it "returns nil" do
161− assert_nil doc.index_page('') {}
162+ it "doesn't store a file" do
163+ dont_allow(store).write
164+ doc.store_page(store, 'id')
162165 end
163166 end
164167 end
@@ -170,13 +173,13 @@ class DocsDocTest < MiniTest::Spec · 5 unmodified lines
170173 end
171174 end
172175
173− it "doesn't yield" do
174− doc.index_page('') { |*_| @yield = true }
175− refute @yield
176+ it "returns false" do
177+ refute doc.store_page(store, 'id')
176178 end
177179
178− it "returns nil" do
179− assert_nil doc.index_page('') {}
180+ it "doesn't store a file" do
181+ dont_allow(store).write
182+ doc.store_page(store, 'id')
180183 end
181184 end
182185 end
@@ -256,48 +259,6 @@ class DocsDocTest < MiniTest::Spec · 73 unmodified lines
256259 end
257260 end
258261
259− describe ".store_page" do
260− context "when the page is indexed successfully" do
261− before do
262− stub(doc).index_page('id').yields(page[:store_path], page[:output]) { index }
263− end
264−
265− it "returns true" do
266− assert doc.store_page(store, 'id')
267− end
268−
269− it "stores a file" do
270− mock(store).write(page[:store_path], page[:output])
271− doc.store_page(store, 'id')
272− end
273−
274− it "opens the .path directory before storing the file" do
275− stub(doc).path { 'path' }
276− stub(store).write { assert false }
277− mock(store).open('path') do |_, block|
278− stub(store).write
279− block.call
280− end
281− doc.store_page(store, 'id')
282− end
283− end
284−
285− context "when the page isn't indexed successfully" do
286− before do
287− stub(doc).index_page('id') { nil }
288− end
289−
290− it "returns false" do
291− refute doc.store_page(store, 'id')
292− end
293−
294− it "doesn't store a file" do
295− dont_allow(store).write
296− doc.store_page(store, 'id')
297− end
298− end
299− end
300−
301262 describe ".store_pages" do
302263 context "when pages are indexed successfully" do
303264 before do