Class: JLDrill::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/jldrill/model/Item.rb

Overview

Holds an item for the quiz. For memory and performance purposes these items are stored as:

o The class of the underlying object
o A string containing the object
o The ItemStatus of the object

The string representation of the object can be obtain through to_s(). The object representation of the object can be obtained through to_o().

Item also holds position information of the item in the drill

* position is the original ordinal position of the item in the quiz
* bin is the number of the bin

Items stored here must implement the following:

o to_s() -- returns a string representation of the object
o create() -- accepts a string and creates the object

Constant Summary collapse

POSITION_RE =
/^Position: (.*)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item = nil) ⇒ Item

Returns a new instance of Item.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jldrill/model/Item.rb', line 33

def initialize(item=nil)
    @quiz = nil
    if item.nil?
        @itemType = nil
        @contents = ""
        @hash = "".hash
    else
        @itemType = item.class
        @contents = item.to_s
        @hash = item.hash
    end
    @position = -1
    @bin = 0
    @container = nil
    @status = ItemStatus.new(self)
    @status.add(ProblemStatus.new(self))
    @status.add(ItemStats.new(self))
    @cache = nil
end

Instance Attribute Details

#binObject

Returns the value of attribute bin.



29
30
31
# File 'lib/jldrill/model/Item.rb', line 29

def bin
  @bin
end

#containerObject

Returns the value of attribute container.



29
30
31
# File 'lib/jldrill/model/Item.rb', line 29

def container
  @container
end

#contentsObject (readonly)

Returns the value of attribute contents.



29
30
31
# File 'lib/jldrill/model/Item.rb', line 29

def contents
  @contents
end

#hashObject (readonly)

Returns the value of attribute hash.



29
30
31
# File 'lib/jldrill/model/Item.rb', line 29

def hash
  @hash
end

#itemTypeObject (readonly)

Returns the value of attribute itemType.



29
30
31
# File 'lib/jldrill/model/Item.rb', line 29

def itemType
  @itemType
end

#positionObject

Returns the value of attribute position.



29
30
31
# File 'lib/jldrill/model/Item.rb', line 29

def position
  @position
end

#quizObject

Returns the value of attribute quiz.



29
30
31
# File 'lib/jldrill/model/Item.rb', line 29

def quiz
  @quiz
end

#statusObject (readonly)

Returns the value of attribute status.



29
30
31
# File 'lib/jldrill/model/Item.rb', line 29

def status
  @status
end

Class Method Details

.create(string) ⇒ Object

Create an item using the save string Note: We are passing bin to this method, since we no longer read it in. Due to legacy issues, the item status needs to know what bin it is in when parsing.



57
58
59
60
61
# File 'lib/jldrill/model/Item.rb', line 57

def Item.create(string)
    item = Item.new
    item.parse(string)
    return item
end

Instance Method Details

#allCorrectObject



143
144
145
146
# File 'lib/jldrill/model/Item.rb', line 143

def allCorrect
    problemStatus = @status.select("ProblemStatus")
    problemStatus.allCorrect     
end

#allIncorrectObject



148
149
150
151
# File 'lib/jldrill/model/Item.rb', line 148

def allIncorrect
    problemStatus = @status.select("ProblemStatus")
    problemStatus.allIncorrect     
end

#allSeen(value) ⇒ Object



128
129
130
131
# File 'lib/jldrill/model/Item.rb', line 128

def allSeen(value)
    problemStatus = @status.select("ProblemStatus")
    problemStatus.allSeen(value)
end

#assign(item) ⇒ Object

Assign the contents of item to this item



163
164
165
166
167
168
169
170
171
# File 'lib/jldrill/model/Item.rb', line 163

def assign(item)
    setType(item.itemType)
    setContents(item.contents)
    @position = item.position
    @bin = item.bin
    @status.assign(item.status)
    @hash = item.hash
    @cache = nil
end

#cloneObject

Create a copy of this item



94
95
96
97
98
# File 'lib/jldrill/model/Item.rb', line 94

def clone
    item = Item.new
    item.assign(self)
    return item
end

#contain?(object) ⇒ Boolean

Returns true if the item contains the object.

Returns:

  • (Boolean)


263
264
265
266
267
268
269
# File 'lib/jldrill/model/Item.rb', line 263

def contain?(object)
    if object.hash == @hash
        self.to_o.eql?(object)
    else
        false
    end
end

#demoteAllObject

Demote all the schedules



118
119
120
121
# File 'lib/jldrill/model/Item.rb', line 118

def demoteAll
    problemStatus = @status.select("ProblemStatus")
    problemStatus.demoteAll
end

#eql?(item) ⇒ Boolean

Returns true if the items contain the same object. Note: Does not compare the status

Returns:

  • (Boolean)


254
255
256
257
258
259
260
# File 'lib/jldrill/model/Item.rb', line 254

def eql?(item)
    if item.hash == @hash
        self.to_o.eql?(item.to_o)
    else
        false
    end
end

#hasKanji?Boolean

Returns:

  • (Boolean)


247
248
249
250
# File 'lib/jldrill/model/Item.rb', line 247

def hasKanji?
    v = to_o
    return !v.kanji.nil?
end

#insertBefore(item) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/jldrill/model/Item.rb', line 203

def insertBefore(item)
    target = item.position
    # This is clearly slow. It can be made slightly
    # faster by only iterating over the relevant
    # items, but I don't know if it's worth the effort
    # since the majority of the cost is in creating the
    # sorted array in the first place.
    @container.eachByPosition do |i|
        if (i.position >= target) &&
                (i.position < @position)
            i.position += 1
        end
    end
    @position = target

    if !@quiz.nil?
        if (@bin == 0) && (item.bin == 0)
            @quiz.contents.bins[@bin].moveBeforeItem(self, item)
        end
        @quiz.setNeedsSave(true)
    end
end

#itemStatsObject



158
159
160
# File 'lib/jldrill/model/Item.rb', line 158

def itemStats
    return @status.select("ItemStats")
end

#parse(string) ⇒ Object

Set the value of the item by parsing the string



86
87
88
89
90
91
# File 'lib/jldrill/model/Item.rb', line 86

def parse(string)
    @itemType = JLDrill::Vocabulary
    @contents = string
    parseLine(@contents)
    @hash = self.to_o.hash
end

#parseLine(line) ⇒ Object

Parse a whole line which includes status information



77
78
79
80
81
82
83
# File 'lib/jldrill/model/Item.rb', line 77

def parseLine(line)
    line.split("/").each do |part|
        if !parsePart(part)
            @status.parse(part)
        end
    end
end

#parsePart(part) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jldrill/model/Item.rb', line 63

def parsePart(part)
    parsed = true

    case part
    when POSITION_RE 
        @position = $1.to_i
    else # Not something we understand
        parsed = false
    end

    return parsed
end

#problemObject



153
154
155
156
# File 'lib/jldrill/model/Item.rb', line 153

def problem
    problemStatus = @status.select("ProblemStatus")
    return problemStatus.firstProblem
end

#problemModified(problem) ⇒ Object

Indicate to the quiz that the problem has been modified This will be called by the problem itself whenever it has been modified.



274
275
276
277
278
# File 'lib/jldrill/model/Item.rb', line 274

def problemModified(problem)
    if !@quiz.nil?
        @quiz.problemModified(problem)
    end
end

#removeInvalidKanjiProblemsObject



100
101
102
103
# File 'lib/jldrill/model/Item.rb', line 100

def removeInvalidKanjiProblems
    problemStatus = @status.select("ProblemStatus")
    problemStatus.removeInvalidKanjiProblems
end

#resetSchedulesObject



123
124
125
126
# File 'lib/jldrill/model/Item.rb', line 123

def resetSchedules
    problemStatus = @status.select("ProblemStatus")
    problemStatus.resetAll
end

#scheduleObject

Return the schedule for the Spaced Repetition Drill



106
107
108
109
# File 'lib/jldrill/model/Item.rb', line 106

def schedule
    problemStatus = @status.select("ProblemStatus")
    return problemStatus.firstSchedule
end

#scheduleAllObject

UpdateAll the schedules



112
113
114
115
# File 'lib/jldrill/model/Item.rb', line 112

def scheduleAll
    problemStatus = @status.select("ProblemStatus")
    problemStatus.scheduleAll
end

#setContents(contents) ⇒ Object

set the contents of the item



184
185
186
187
# File 'lib/jldrill/model/Item.rb', line 184

def setContents(contents)
    @contents = contents
    @hash = to_o.hash
end

#setLevels(value) ⇒ Object



138
139
140
141
# File 'lib/jldrill/model/Item.rb', line 138

def setLevels(value)
    problemStatus = @status.select("ProblemStatus")
    problemStatus.setLevels(value)     
end

#setScores(value) ⇒ Object



133
134
135
136
# File 'lib/jldrill/model/Item.rb', line 133

def setScores(value)
    problemStatus = @status.select("ProblemStatus")
    problemStatus.setScores(value)     
end

#setStatus(status) ⇒ Object

set the ItemStatus



179
180
181
# File 'lib/jldrill/model/Item.rb', line 179

def setStatus(status)
    parseLine(status.to_s)
end

#setType(aType) ⇒ Object

Set the type of the item



174
175
176
# File 'lib/jldrill/model/Item.rb', line 174

def setType(aType)
    @itemType = aType
end

#swapWith(item) ⇒ Object

swap the positions between two items



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/jldrill/model/Item.rb', line 190

def swapWith(item)
    temp = @position
    @position = item.position
    item.position = temp
   
    if !@quiz.nil?
        if (@bin == 0) && (item.bin == 0)
            @quiz.contents.bins[@bin].moveBeforeItem(self, item)
        end
        @quiz.setNeedsSave(true)
    end
end

#to_oObject

Create the object in the item and return it



236
237
238
239
240
241
242
243
244
245
# File 'lib/jldrill/model/Item.rb', line 236

def to_o
    if !@contents.empty?
        if @cache.nil?
            @cache = @itemType.create(@contents)
        end
    else
        @cache = nil
    end
    return @cache
end

#to_sObject

Return the save format of the item



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

def to_s
    retVal = to_o.to_s
    retVal += "/Position: #{@position}"
    retVal += @status.to_s 
    retVal += "/\n"
    return retVal
end