Class: SRS::Models::SimpleFlashcard

Inherits:
Object
  • Object
show all
Defined in:
lib/srs/models/SimpleFlashcard.rb

Instance Method Summary collapse

Constructor Details

#initialize ⇒ SimpleFlashcard

Returns a new instance of SimpleFlashcard.



6
7
# File 'lib/srs/models/SimpleFlashcard.rb', line 6

def initialize()
end

Instance Method Details

#load(datafile) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/srs/models/SimpleFlashcard.rb', line 67

def load(datafile)
	@fields = {}
	File.open(datafile, "r") do |file|
		while( line = file.gets() ) do
			if line.strip.empty? then
				break
			end

			keyval = line.split(':').map{|e| e.strip}
			key = keyval[0]
			val = keyval[1]

			@fields[key] = val
		end
	end
end

#run(headers, metadata) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/srs/models/SimpleFlashcard.rb', line 9

def run(headers, )
	data = headers.delete("Data")

	sha1_start = data[0..1]
	sha1_rest = data[2..-1]

	datafile = "data/#{sha1_start}/#{sha1_rest}"

	if not File.exists?(datafile) then
		puts "No content with that ID exists"
		return 4
	end

	self.load(datafile)

	score = 0.0
	StringIO.open() do ||
		while( line = .gets() ) do
			break if line.strip == "---"
			line.gsub!(/\[([^\]]+)\]/) { "#{@fields[$1]}" }
			puts line
		end
		answer = .read.strip.gsub!(/\[([^\]]+)\]/) { "#{@fields[$1]}" }

		print "> "
		attempt = STDIN.gets().strip

		if( attempt == answer ) then
			puts "Correct."
			score = 1.0
		else
			puts answer

			for i in 0..0
				puts "Was your answer: [h] Correct, [j] Close, [k] Wrong, or [l] Very Wrong?"
				print "> "

				case STDIN.gets().strip
				when "h"
					score = 1.0
				when "j"
					score = 0.8
				when "k"
					score = 0.4
				when "l"
					score = 0.0
				else
					redo
				end
			end
		end
	end

	puts "You scored: " + score.to_s

	return score
end