Class: JLDrill::Quiz

Inherits:
DataFile show all
Defined in:
lib/jldrill/model/Quiz/Quiz.rb

Constant Summary collapse

JLDRILL_HEADER_RE =
/^(\d+\.\d+\.\d+)?-?LDRILL-SAVE (.*)/
COMMENT_RE =
/^\#[ ]?(.*)/
VERSION_RE =
/^(\d+)\.(\d+)\.(\d+)/
JLDRILL_CANLOAD_RE =
/^(\d+\.\d+\.\d+)?-?LDRILL-SAVE/

Instance Attribute Summary collapse

Attributes inherited from DataFile

#encoding, #file, #lines, #parsed, #publisher, #stepSize

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DataFile

#createLines, #eof?, #findEncoding, #fraction, #load, #loaded?, #parse, #parseChunk, #parser, #readLines, #shortFilename

Constructor Details

#initializeQuiz

Returns a new instance of Quiz.



24
25
26
27
28
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 24

def initialize()
    super
    # Make the file progress indicator report every 10 lines
    @stepSize = 10
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



19
20
21
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 19

def contents
  @contents
end

#currentProblemObject

Returns the value of attribute currentProblem.



19
20
21
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 19

def currentProblem
  @currentProblem
end

#infoObject

Returns the value of attribute info.



19
20
21
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 19

def info
  @info
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 19

def name
  @name
end

#needsSaveObject (readonly)

Returns the value of attribute needsSave.



19
20
21
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 19

def needsSave
  @needsSave
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 19

def options
  @options
end

#strategyObject (readonly)

Returns the value of attribute strategy.



19
20
21
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 19

def strategy
  @strategy
end

Class Method Details

.canLoad?(header) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 160

def Quiz.canLoad?(header)
    retVal = false
    if header =~ JLDRILL_CANLOAD_RE
        if $1 != ""
            if $1 =~ VERSION_RE
                if $1.to_i > 0 || $2.to_i < 7
                    retVal = true
                end
            end
        end
    end
    return retVal
end

.drillFile?(file) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
181
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 174

def Quiz.drillFile?(file)
    retVal = false
    loadFile = File.new(file, "r")
    if(loadFile)
        retVal = Quiz.canLoad?(loadFile.readline)
    end
    return retVal
end

Instance Method Details

#allItemsObject

Get an array containing all the items in the quiz



304
305
306
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 304

def allItems
    @contents.allItems
end

#answerObject



390
391
392
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 390

def answer()
    @currentProblem.answer
end

#append(quiz) ⇒ Object

Append any new items in the quiz to this quiz Note: Does not add items that are already existing.

nor does it update the status of existing items


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 254

def append(quiz)
    # Don't update the status while we're appending the file
    @publisher.block
    lastItem = @contents.addContents(quiz.contents)
    # Update status again
    @publisher.unblock
    # The quiz has been modified
    update
    # A file has been loaded
    updateLoad
    # Indicate the last item that was loaded
    updateItemAdded(lastItem) unless lastItem.nil?
    if @currentProblem.nil?
        # Drill a problem if there wasn't one before
        drill
    end
end

#appendVocab(vocab) ⇒ Object



282
283
284
285
286
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 282

def appendVocab(vocab)
    item = Item.new(vocab)
    @contents.addUniquely(item)
    return item
end

#binObject



51
52
53
54
55
56
57
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 51

def bin
    retVal = nil
    if !@currentProblem.nil?
        retVal = @currentProblem.item.bin
    end
    retVal
end

#correctObject



330
331
332
333
334
335
336
337
338
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 330

def correct
    if !@currentProblem.nil?
        item = @currentProblem.item
        if !item.nil?
            @strategy.correct(item)
            setNeedsSave(true)
        end
    end
end

#createProblem(item) ⇒ Object

Creates a problem to be quizzed



360
361
362
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 360

def createProblem(item)
    setCurrentProblem(@strategy.createProblem(item))
end

#currentAnswerObject



398
399
400
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 398

def currentAnswer
    @currentProblem.answer
end

#currentDrillObject



394
395
396
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 394

def currentDrill
    @currentProblem.question
end

#dataSizeObject



211
212
213
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 211

def dataSize
    @contents.size
end

#deleteItem(item) ⇒ Object

Delete an item from the quiz



315
316
317
318
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 315

def deleteItem(item)
    @contents.delete(item)
    updateItemDeleted(item)
end

#displayProblem(item) ⇒ Object

Creates a problem to be displayed only



365
366
367
368
369
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 365

def displayProblem(item)
    problem = @strategy.createProblem(item)
    problem.setDisplayOnly(true)
    setCurrentProblem(problem)
end

#drillObject



383
384
385
386
387
388
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 383

def drill()
    item = @strategy.getItem
    if !item.nil?
        createProblem(item)
    end
end

#exists?(vocab) ⇒ Boolean

Returns true if the vocabulary already exists in the Quiz

Returns:

  • (Boolean)


278
279
280
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 278

def exists?(vocab)
    @contents.exists?(vocab)
end

#fileHeaderObject



114
115
116
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 114

def fileHeader
    JLDrill::VERSION + "-LDRILL-SAVE #{@name}\n"
end

#finishParsingObject



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 215

def finishParsing
    # Need to sort the new set to deal with older files that
    # may not be sorted.
    @strategy.newSet.sort! do |x, y|
        x.position <=> y.position
    end
    # Resort the review set according to schedule
    reschedule
    setNeedsSave(true)
    update
    super
end

#incorrectObject



320
321
322
323
324
325
326
327
328
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 320

def incorrect
    if !@currentProblem.nil?
        item = @currentProblem.item
        if !item.nil?
            @strategy.incorrect(item)
            setNeedsSave(true)
        end
    end
end

#learnObject

Promote the current problem into the review set if it is in the working set without any further practice.



343
344
345
346
347
348
349
350
351
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 343

def learn
    if !@currentProblem.nil?
        item = @currentProblem.item
        if !item.nil?
            @strategy.learn(item)
            setNeedsSave(true)
        end
    end
end

#lengthObject



43
44
45
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 43

def length
    @contents.length
end

#loadFromDict(dict) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 235

def loadFromDict(dict)
    if dict
        # Don't update the status while we're loading the file
        @publisher.block
        reset
        @name = dict.shortFilename
        dict.eachVocab do |vocab|
            @contents.add(vocab, 0)
        end
        reschedule
        # Update status again
        @publisher.unblock
        setNeedsSave(true)
    end
end

#loadFromString(file, string) ⇒ Object



228
229
230
231
232
233
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 228

def loadFromString(file, string)
    reset
    @file = file
    @lines = string.split("\n")
    parse
end

#needsSave?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 110

def needsSave?
    @needsSave
end

#parseEntryObject



206
207
208
209
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 206

def parseEntry
    parseLine(@lines[@parsed])
    @parsed += 1
end

#parseLine(line) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 183

def parseLine(line)
    # These are put in a specific order for performance
    # By checking the most common items first we avoid doing
    # needless regular expression checks.  It's not a huge
    # savings, but it helps for very big files.
    # Normal contents are the most common
    if !@contents.parseLine(line)
        # Quiz options are the next most common
        if !@options.parseLine(line)
            # Comments, headers and unparsable lines are 
            # the least common
            case line
            when JLDRILL_HEADER_RE
                @name = $2
            when COMMENT_RE
                @info += $1 + "\n"
            else 
                # Ignore things we don't understand
            end
        end
    end
end

#previewProblem(item) ⇒ Object

Creates a preview for a problem



372
373
374
375
376
377
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 372

def previewProblem(item)
    problem = @strategy.createProblem(item)
    problem.setDisplayOnly(true)
    problem.setPreview(true)
    setCurrentProblem(problem)
end

#problemModified(problem) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 93

def problemModified(problem)
    @publisher.update("problemModified", problem)
    setNeedsSave(true)
    if !problem.valid? &&
            !@currentProblem.nil? && (problem == @currentProblem)
        # The current problem has been edited and can't be displayed
        # like it is (i.e., A Kanji problem has had it's kanji removed)
        # Recreate it.  
        recreateProblem
    end
end

#recreateProblemObject



379
380
381
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 379

def recreateProblem
    createProblem(@currentProblem.item) unless @currentProblem.nil?
end

#rescheduleObject

Sort the items in the ReviewSet according to their schedule



273
274
275
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 273

def reschedule
    @strategy.reschedule
end

#resetObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 30

def reset
    @name = ""
    @info = ""
    @options = Options.new(self)
    @contents = Contents.new(self)
    @strategy = Strategy.new(self)
    @currentProblem = nil
    @needsSave = false
    @last = nil
    update
    super
end

#resetContentsObject

Resets the quiz back to it’s original state



309
310
311
312
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 309

def resetContents
    @contents.reset()
    drill()
end

#saveObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 128

def save
    if (@contents.length == 0) || !@needsSave
        return true
    elsif @file == "" 
        return false
    else
        begin
            saveFile = File.new(@file, "w")
            if saveFile
                saveFile.print(saveToString)
                saveFile.close
                setNeedsSave(false)
                retVal = true
            end
        rescue
            return false
        end
    end
end

#saveToStringObject



118
119
120
121
122
123
124
125
126
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 118

def saveToString
    retVal = fileHeader
    @info.split("\n").each { |line|
        retVal += "# " + line + "\n"
    }
    retVal += @options.to_s
    retVal += @contents.to_s
    retVal
end

#setCurrentProblem(problem) ⇒ Object



353
354
355
356
357
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 353

def setCurrentProblem(problem)
    @currentProblem = problem
    update
    updateNewProblem(@currentProblem)
end

#setLoaded(bool) ⇒ Object



75
76
77
78
79
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 75

def setLoaded(bool)
    if bool
        updateLoad
    end
end

#setNeedsSave(bool) ⇒ Object



105
106
107
108
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 105

def setNeedsSave(bool)
    @needsSave = bool
    update
end

#sizeObject



47
48
49
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 47

def size
    length
end

#statusObject



288
289
290
291
292
293
294
295
296
297
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 288

def status
    retVal = ""
    if(@needsSave) then retVal += "* " else retVal += "  " end
    retVal += @contents.status + " "
    if !@currentProblem.nil?
        retVal += @currentProblem.status + " "
    end
    retVal += @strategy.status
    return retVal
end

#subscribe(subscriber) ⇒ Object



59
60
61
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 59

def subscribe(subscriber)
    @publisher.subscribe(subscriber, "quiz")
end

#to_sObject



299
300
301
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 299

def to_s
    status
end

#unsubscribe(subscriber) ⇒ Object



63
64
65
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 63

def unsubscribe(subscriber)
    @publisher.unsubscribe(subscriber, "quiz")
end

#updateObject



67
68
69
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 67

def update
    @publisher.update("quiz")
end

#updateItemAdded(item) ⇒ Object



85
86
87
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 85

def updateItemAdded(item)
    @publisher.update("itemAdded", item)
end

#updateItemDeleted(item) ⇒ Object



81
82
83
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 81

def updateItemDeleted(item)
    @publisher.update("itemDeleted", item)
end

#updateLoadObject



71
72
73
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 71

def updateLoad
    @publisher.update("load")
end

#updateNewProblem(problem) ⇒ Object



89
90
91
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 89

def updateNewProblem(problem)
    @publisher.update("newProblem", problem)
end

#useSavePath(filename) ⇒ Object

Returns the filename relative to the Quiz’s current path. If a filename hasn’t been set, the file is expanded using the applications current path.



151
152
153
154
155
156
157
158
# File 'lib/jldrill/model/Quiz/Quiz.rb', line 151

def useSavePath(filename)
    if !@file.empty?
        dirname = File.expand_path(File.dirname(@file))
        return File.expand_path(filename, dirname)
    else
        return File.expand_path(filename)
    end
end