Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/jldrill/model/DataFile.rb | 151 | 94 | 93.38%
|
90.43%
|
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/Publisher' |
2 |
3 module JLDrill |
4 # This class represents a data file in JLDrill. This |
5 # is an abstract class meant to define the interface |
6 # for having a file which can be read in the background |
7 # in JLDrill. |
8 class DataFile |
9 attr_reader :file, :lines, :parsed, :publisher, :stepSize |
10 attr_writer :lines, :stepSize |
11 |
12 def initialize |
13 @publisher = Context::Publisher.new(self) |
14 # Default to reporting every 100 lines |
15 @stepSize = 100 |
16 self.reset |
17 end |
18 |
19 # Returns the number of items you have created |
20 def dataSize |
21 # Please implement this in the concrete class |
22 end |
23 |
24 # Returns a reference to the object that can parse a line |
25 def parser |
26 # Please implement this in the concrete class unless |
27 # you modify the parseEntry() method to directly access |
28 # the parser. |
29 end |
30 |
31 # Resets the file |
32 def reset |
33 @file = "" |
34 @lines = [] |
35 @parsed = 0 |
36 setLoaded(false) |
37 # Please define the rest of the method and call super() |
38 # at the end. |
39 end |
40 |
41 # Sets the filename of the file and resets the data. |
42 def file=(filename) |
43 if @file != filename |
44 @file = filename |
45 end |
46 end |
47 |
48 # Indicate to the outside world that the file is loaded |
49 def setLoaded(bool) |
50 if bool |
51 @publisher.update("loaded") |
52 end |
53 end |
54 |
55 # Returns true if there is no more data to parse |
56 def eof? |
57 return @parsed >= @lines.size |
58 end |
59 |
60 # Returns true if the we have completed parsing a file |
61 def loaded? |
62 return eof? && (dataSize > 0) |
63 end |
64 |
65 # Returns a float showing the percentage of the file that |
66 # has been parsed so far. |
67 def fraction |
68 retVal = 0.0 |
69 if @lines.size != 0 |
70 retVal = @parsed.to_f / @lines.size.to_f |
71 end |
72 return retVal |
73 end |
74 |
75 # Read the file into memory. This is done before parsing |
76 def readLines |
77 begin |
78 @lines = IO.readlines(@file) |
79 rescue |
80 Context::Log::warning("JLDrill::DataFile", |
81 "Could not load #{@file}.") |
82 @lines = [] |
83 end |
84 @parsed = 0 |
85 end |
86 |
87 # Load in the file data, but don't parse it yet |
88 def load(file) |
89 reset |
90 @file = file |
91 readLines |
92 end |
93 |
94 # Parse the entire file all at once |
95 def parse |
96 parseChunk(@lines.size) |
97 end |
98 |
99 # Parses one entry from the lines. |
100 # The default parses a single line from the lines. |
101 # You can override this for files whose entries span more than one line. |
102 def parseEntry |
103 parser.parse(@lines[@parsed]) |
104 @parsed += 1 |
105 end |
106 |
107 # Parse a chunk of the file. Size shows how many entries |
108 # to parse |
109 def parseChunk(size) |
110 # We don't want to get updated when we parse a large block of data |
111 @publisher.block |
112 |
113 last = @parsed + size |
114 if last > @lines.size |
115 last = @lines.size |
116 end |
117 while @parsed < last do |
118 parseEntry |
119 end |
120 @publisher.unblock |
121 |
122 # If the parsing is finished dispose of the unparsed lines |
123 finished = self.eof? |
124 if finished |
125 finishParsing |
126 end |
127 |
128 return finished |
129 end |
130 |
131 # Usually we want to delete the original source lines when |
132 # we are finished parsing. But some files are only |
133 # partially parsed on reading (like Edict). |
134 # Please redefine this if you want to keep the source |
135 # lines around for some reason. |
136 def finishParsing |
137 @lines = [] |
138 @parsed = 0 |
139 setLoaded(true) |
140 end |
141 |
142 # Returns the filename without the path |
143 def shortFilename |
144 if @file.nil? || @file.empty? |
145 return "No name" |
146 end |
147 return File.basename(file) |
148 end |
149 |
150 end |
151 end |
Generated on Mon May 23 16:17:46 +0900 2011 with rcov 0.9.8