Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/jldrill/contexts/MainContext.rb | 391 | 329 | 62.15%
|
56.53%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 require 'Context/Context' |
2 require 'Context/Bridge' |
3 require 'Context/View' |
4 require 'jldrill/model/Quiz/Quiz' |
5 require 'jldrill/model/items/JEDictionary' |
6 require 'jldrill/contexts/RunCommandContext' |
7 require 'jldrill/contexts/LoadReferenceContext' |
8 require 'jldrill/contexts/SetOptionsContext' |
9 require 'jldrill/contexts/ShowStatisticsContext' |
10 require 'jldrill/contexts/GetFilenameContext' |
11 require 'jldrill/contexts/AddNewVocabularyContext' |
12 require 'jldrill/contexts/EditVocabularyContext' |
13 require 'jldrill/contexts/DisplayQuizStatusContext' |
14 require 'jldrill/contexts/DisplayProblemContext' |
15 require 'jldrill/contexts/PromptForSaveContext' |
16 require 'jldrill/contexts/PromptForDeleteContext' |
17 require 'jldrill/contexts/ShowInfoContext' |
18 require 'jldrill/contexts/ShowAllVocabularyContext' |
19 require 'jldrill/contexts/LoadTanakaContext' |
20 require 'jldrill/contexts/LoadQuizContext' |
21 require 'jldrill/contexts/LoadKanjiContext' |
22 require 'jldrill/contexts/AppendFileContext' |
23 require 'jldrill/model/Acknowlegements' |
24 require 'jldrill/contexts/ShowAboutContext' |
25 require 'jldrill/contexts/ShowExamplesContext' |
26 require 'jldrill/model/moji/Radical' |
27 require 'jldrill/model/moji/Kanji' |
28 require 'jldrill/model/moji/Kana' |
29 require 'jldrill/model/Tanaka' |
30 require 'jldrill/model/DeinflectionRules' |
31 |
32 module JLDrill |
33 |
34 class MainContext < Context::Context |
35 |
36 attr_reader :loadReferenceContext, :setOptionsContext, |
37 :showStatisticsContext, :getFilenameContext, |
38 :addNewVocabularyContext, :displayQuizStatusContext, |
39 :displayProblemContext, :runCommandContext, |
40 :showInfoContext, :showAllVocabularyContext, |
41 :showAboutContext, :editVocabularyContext, |
42 :loadTanakaContext, :showExamplesContext, |
43 :loadQuizContext, :loadKanjiContext, |
44 :appendFileContext, |
45 :reference, :quiz, :kanji, :radicals, :kana, |
46 :inTests, :tanaka, :deinflect |
47 |
48 attr_writer :quiz, :inTests |
49 |
50 def initialize(viewBridge) |
51 super(viewBridge) |
52 @runCommandContext = RunCommandContext.new(viewBridge) |
53 @loadReferenceContext = LoadReferenceContext.new(viewBridge) |
54 @setOptionsContext = SetOptionsContext.new(viewBridge) |
55 @showStatisticsContext = ShowStatisticsContext.new(viewBridge) |
56 @getFilenameContext = GetFilenameContext.new(viewBridge) |
57 @getFilenameContext.directory = File.join(JLDrill::Config::DATA_DIR, "quiz") |
58 @addNewVocabularyContext = AddNewVocabularyContext.new(viewBridge) |
59 @editVocabularyContext = EditVocabularyContext.new(viewBridge) |
60 @displayQuizStatusContext = DisplayQuizStatusContext.new(viewBridge) |
61 @displayProblemContext = DisplayProblemContext.new(viewBridge) |
62 @showInfoContext = ShowInfoContext.new(viewBridge) |
63 @showAllVocabularyContext = ShowAllVocabularyContext.new(viewBridge) |
64 @showAboutContext = ShowAboutContext.new(viewBridge) |
65 @loadTanakaContext = LoadTanakaContext.new(viewBridge) |
66 @showExamplesContext = ShowExamplesContext.new(viewBridge) |
67 @loadQuizContext = LoadQuizContext.new(viewBridge) |
68 @loadKanjiContext = LoadKanjiContext.new(viewBridge) |
69 @appendFileContext = AppendFileContext.new(viewBridge) |
70 @reference = JEDictionary.new |
71 @kanji = KanjiFile.new |
72 @radicals = RadicalFile.new |
73 @kana = KanaFile.new |
74 @quiz = Quiz.new |
75 # The quiz doesn't need to be saved |
76 @quiz.setNeedsSave(false) |
77 @inTests = false |
78 @tanaka = Tanaka::Reference.new |
79 @deinflect = DeinflectionRulesFile.new |
80 end |
81 |
82 class MainWindowView < Context::View |
83 def inititalize(context) |
84 super(context) |
85 end |
86 |
87 # Destroy the main window |
88 def destroy |
89 # define in the concrete class |
90 end |
91 |
92 # This is a convenience method for the tests so that |
93 # they don't have to catch the exit on the context. |
94 def close |
95 context.exit |
96 end |
97 end |
98 |
99 def createViews |
100 @mainWindowView = @viewBridge.MainWindowView.new(self) |
101 @mainView = @mainWindowView |
102 end |
103 |
104 def destroyViews |
105 @mainWindowView.destroy unless @mainWindowView.nil? |
106 @mainWindowView = nil |
107 @mainView = nil |
108 end |
109 |
110 def parseCommandLineOptions |
111 if ARGV.size == 1 |
112 openFile(ARGV[0]) |
113 end |
114 end |
115 |
116 def enter(parent) |
117 super(parent) |
118 @runCommandContext.enter(self) |
119 @displayProblemContext.enter(self) |
120 @displayQuizStatusContext.enter(self) |
121 parseCommandLineOptions |
122 @quiz.options.subscribe(self) |
123 loadKanji unless @inTests |
124 end |
125 |
126 def exit |
127 @runCommandContext.exit |
128 @displayQuizStatusContext.exit |
129 @displayProblemContext.exit |
130 @quiz.options.unsubscribe(self) |
131 @parent.exit |
132 super |
133 end |
134 |
135 def optionsUpdated(options) |
136 if options.autoloadDic |
137 loadReference |
138 end |
139 end |
140 |
141 def save |
142 if @quiz.file.empty? |
143 saveAs |
144 else |
145 if !@quiz.save |
146 print "Error: Can't save. Try again.\n" |
147 saveAs |
148 end |
149 end |
150 end |
151 |
152 def saveAs |
153 filename = @getFilenameContext.enter(self, GetFilenameContext::SAVE) |
154 if !filename.nil? |
155 @quiz.file = filename |
156 while !@quiz.save |
157 print "Error: Can't save. Try again.\n" |
158 end |
159 end |
160 end |
161 |
162 def openFile(filename = nil) |
163 if !@loadQuizContext.isEntered? |
164 @loadQuizContext.onExit do |
165 @quiz.options.subscribe(self) |
166 optionsUpdated(@quiz.options) |
167 @quiz.drill |
168 end |
169 @loadQuizContext.enter(self, @quiz, filename) |
170 end |
171 end |
172 |
173 def appendFile |
174 if !@appendFileContext.isEntered? |
175 @appendFileContext.onExit do |
176 if quiz.currentProblem.nil? |
177 quiz.drill |
178 end |
179 end |
180 @appendFileContext.enter(self, @quiz) |
181 end |
182 end |
183 |
184 def promptForSaveAnd(&block) |
185 if @quiz.needsSave? |
186 promptForSave = PromptForSaveContext.new(@viewBridge) |
187 result = promptForSave.enter(self) |
188 if result == promptForSave.yes |
189 save |
190 block.call |
191 elsif result == promptForSave.no |
192 block.call |
193 else |
194 # Cancel... Do nothing |
195 end |
196 else |
197 block.call |
198 end |
199 end |
200 |
201 def createNew |
202 promptForSaveAnd do |
203 @quiz.setup |
204 # We need to resubscribe to the options in the new quiz |
205 # and realize that the options may have changed. |
206 @quiz.options.subscribe(self) |
207 optionsUpdated(@quiz.options) |
208 # New quizes don't need to be saved. |
209 @quiz.setNeedsSave(false) |
210 end |
211 end |
212 |
213 def open |
214 promptForSaveAnd do |
215 openFile |
216 end |
217 end |
218 |
219 def quit |
220 promptForSaveAnd do |
221 exit |
222 end |
223 end |
224 |
225 def loadReference |
226 if !@loadReferenceContext.isEntered? |
227 @loadReferenceContext.enter(self, @reference, @deinflect, |
228 @quiz.options) |
229 end |
230 end |
231 |
232 def loadTanaka |
233 if @tanaka.loaded? |
234 if !@showExamplesContext.isEntered? |
235 @showExamplesContext.enter(self) |
236 end |
237 else |
238 if !@loadTanakaContext.isEntered? |
239 @loadTanakaContext.onExit do |
240 @showExamplesContext.enter(self) |
241 end |
242 @loadTanakaContext.enter(self, @tanaka, @quiz.options) |
243 end |
244 end |
245 end |
246 |
247 |
248 def loadKanji |
249 if !@loadKanjiContext.isEntered? |
250 @loadKanjiContext.enter(self, @kanji, @radicals, @kana) |
251 end |
252 end |
253 |
254 def setOptions |
255 @setOptionsContext.enter(self) unless @setOptionsContext.isEntered? |
256 end |
257 |
258 def showStatistics |
259 @showStatisticsContext.enter(self) unless @showStatisticsContext.isEntered? |
260 end |
261 |
262 def setReviewMode(bool) |
263 @quiz.options.reviewMode = bool unless @quiz.nil? |
264 end |
265 |
266 def addNewVocabulary |
267 @addNewVocabularyContext.enter(self) unless @addNewVocabularyContext.isEntered? |
268 end |
269 |
270 def editVocabulary |
271 if !@quiz.currentProblem.nil? && |
272 !@quiz.currentProblem.preview? |
273 # Always show the answer before editing the problem |
274 showAnswer |
275 @editVocabularyContext.enter(self) unless @editVocabularyContext.isEntered? |
276 end |
277 end |
278 |
279 def deleteVocabulary |
280 if !@quiz.currentProblem.nil? && |
281 !@quiz.currentProblem.preview? |
282 # Always show the answer before deleting an item |
283 showAnswer |
284 prompt = PromptForDeleteContext.new(@viewBridge) |
285 if prompt.enter(self) == prompt.yes |
286 item = @quiz.currentProblem.item |
287 @quiz.deleteItem(item) |
288 drill |
289 end |
290 end |
291 end |
292 |
293 # Display the problem if it isn't the current one |
294 def displayItem(item) |
295 if !item.nil? |
296 if @quiz.currentProblem.nil? || |
297 !@quiz.currentProblem.item.eql?(item) |
298 @quiz.displayProblem(item) |
299 showAnswer |
300 end |
301 end |
302 end |
303 |
304 # Preview an item that doesn't currently exist in the quiz |
305 def previewItem(item) |
306 if !item.nil? |
307 if @quiz.currentProblem.nil? || |
308 !@quiz.currentProblem.item.eql?(item) |
309 @quiz.previewProblem(item) |
310 showAnswer |
311 end |
312 end |
313 end |
314 |
315 def editItem(item) |
316 if !item.nil? |
317 displayItem(item) |
318 editVocabulary |
319 end |
320 end |
321 |
322 def deleteItem(item) |
323 if !item.nil? |
324 displayItem(item) |
325 deleteVocabulary |
326 end |
327 end |
328 |
329 def updateQuizStatus |
330 @displayQuizStatusContext.quizUpdated(@quiz) if @displayQuizStatusContext.isEntered? |
331 end |
332 |
333 def showAnswer |
334 if !@quiz.currentProblem.nil? |
335 @displayProblemContext.showAnswer if @displayProblemContext.isEntered? |
336 end |
337 end |
338 |
339 # Get a new problem in the drill without answering the current problem |
340 def drill |
341 @quiz.drill |
342 end |
343 |
344 def correct |
345 if !@quiz.currentProblem.nil? && !@quiz.currentProblem.displayOnly? |
346 @quiz.correct |
347 @quiz.drill |
348 end |
349 end |
350 |
351 def incorrect |
352 if !@quiz.currentProblem.nil? && !@quiz.currentProblem.displayOnly? |
353 @quiz.incorrect |
354 @quiz.drill |
355 end |
356 end |
357 |
358 def learn |
359 if !@quiz.currentProblem.nil? && !@quiz.currentProblem.displayOnly? |
360 @quiz.learn |
361 @quiz.drill |
362 end |
363 end |
364 |
365 def removeDups |
366 if !quiz.nil? |
367 @quiz.contents.removeDuplicates |
368 end |
369 end |
370 |
371 def reset |
372 @quiz.resetContents |
373 end |
374 |
375 def showQuizInfo |
376 @showInfoContext.enter(self, @quiz.info) unless @showInfoContext.isEntered? |
377 end |
378 |
379 def showAcknowlegements |
380 @showInfoContext.enter(self, Acknowlegements) unless @showInfoContext.isEntered? |
381 end |
382 |
383 def showAllVocabulary |
384 @showAllVocabularyContext.enter(self) unless @showAllVocabularyContext.isEntered? |
385 end |
386 |
387 def showAbout |
388 @showAboutContext.enter(self) unless @showAboutContext.isEntered? |
389 end |
390 end |
391 end |
Generated on Mon May 23 16:17:46 +0900 2011 with rcov 0.9.8