Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/jldrill/model/Bin.rb | 204 | 148 | 81.37%
|
74.32%
|
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 module JLDrill |
2 # Holds a group of items that are at the same level. |
3 class Bin |
4 attr_reader :name, :number, :contents |
5 |
6 # Create a new bin and call it name |
7 def initialize(name, number) |
8 @name = name |
9 @number = number |
10 @contents = [] |
11 end |
12 |
13 # Returns the number of items in the bin |
14 def length() |
15 return @contents.length |
16 end |
17 |
18 # Returns the item at the index specified |
19 def [](index) |
20 item = @contents[index] |
21 return item |
22 end |
23 |
24 def last |
25 if length > 0 |
26 item = @contents[length - 1] |
27 return item |
28 else |
29 return nil |
30 end |
31 end |
32 |
33 # Pushes a item to the end of the bin |
34 # Also sets the bin number of the item |
35 def push(item) |
36 item.bin = @number |
37 @contents.push(item) |
38 end |
39 |
40 # Insert an item before the index indicated |
41 def insertAt(index, item) |
42 if index >= @contents.size |
43 @contents.push(item) |
44 else |
45 @contents.insert(index, item) |
46 end |
47 item.bin = @number |
48 end |
49 |
50 def moveBeforeItem(moveItem, beforeItem) |
51 index = @contents.find_index(beforeItem) |
52 if !index.nil? |
53 delete(moveItem) |
54 insertAt(index, moveItem) |
55 end |
56 end |
57 |
58 # Inserts an item before the one where |
59 # the block evaluates true. If the block |
60 # never evaluates true, put the item at |
61 # the end |
62 def insertBefore(item, &block) |
63 i = 0 |
64 while(!contents[i].nil? && !block.call(i)) |
65 i += 1 |
66 end |
67 insertAt(i, item) |
68 end |
69 |
70 def delete(item) |
71 @contents.delete(item) |
72 end |
73 |
74 # Calls a block for each item in the bin |
75 def each(&block) |
76 @contents.each do |item| |
77 block.call(item) |
78 end |
79 end |
80 |
81 # Calls a block for each item in the bin in the reverse order |
82 def reverse_each(&block) |
83 @contents.reverse_each do |item| |
84 block.call(item) |
85 end |
86 end |
87 |
88 # Calls a block for each item in the bin, stopping if the |
89 # block returns false. Returns true if all iterations return true. |
90 def all?(&block) |
91 @contents.all? do |item| |
92 block.call(item) |
93 end |
94 end |
95 |
96 # Sorts the bin according to the criteria specified in the |
97 # passed in block |
98 def sort!(&block) |
99 @contents.sort! do |x,y| |
100 block.call(x,y) |
101 end |
102 end |
103 |
104 # Returns an array of items for which block returns true |
105 def findAll(&block) |
106 retVal = [] |
107 @contents.each do |item| |
108 if block.call(item) |
109 retVal.push(item) |
110 end |
111 end |
112 return retVal |
113 end |
114 |
115 # Set the contents array to the value specified. Also set the bin |
116 # number correctly |
117 def contents=(array) |
118 @contents = array |
119 self.each do |item| |
120 item.bin = @number |
121 end |
122 end |
123 |
124 # Returns true if the bin is empty |
125 def empty? |
126 @contents.empty? |
127 end |
128 |
129 # Returns the number of unseen items in the bin |
130 def numUnseen |
131 total = 0 |
132 @contents.each do |item| |
133 total += 1 if !item.schedule.seen |
134 end |
135 total |
136 end |
137 |
138 # Returns true if all the items in the bin have been seen |
139 def allSeen? |
140 @contents.all? do |item| |
141 item.schedule.seen? |
142 end |
143 end |
144 |
145 # Return the index of the first item in the bin that hasn't been |
146 # seen yet. Returns -1 if there are no unseen items |
147 def firstUnseen |
148 index = 0 |
149 # find the first one that hasn't been seen yet |
150 while (index < length) && @contents[index].schedule.seen? |
151 index += 1 |
152 end |
153 |
154 if index >= length |
155 index = -1 |
156 end |
157 index |
158 end |
159 |
160 # Return the nth unseen item in the bin |
161 def findUnseen(n) |
162 retVal = nil |
163 if n < numUnseen |
164 i = 0 |
165 0.upto(n) do |m| |
166 while @contents[i].schedule.seen |
167 i += 1 |
168 end |
169 if m != n |
170 i += 1 |
171 end |
172 end |
173 retVal = @contents[i] |
174 end |
175 retVal |
176 end |
177 |
178 # Sets the schedule of each item in the bin to unseen |
179 def setUnseen |
180 @contents.each do |item| |
181 item.schedule.seen = false |
182 end |
183 end |
184 |
185 # Returns true if the Item exists in the bin |
186 def exists?(item) |
187 !@contents.find do |x| |
188 item.eql?(x) |
189 end.nil? |
190 end |
191 |
192 # Returns true if there is an Item in the bin that contains the object |
193 def contain?(object) |
194 !@contents.find do |x| |
195 x.contain?(object) |
196 end.nil? |
197 end |
198 |
199 # Returns a string containing all the item strings in the bin |
200 def to_s |
201 @name + "\n" + @contents.join |
202 end |
203 end |
204 end |
Generated on Mon May 23 16:17:46 +0900 2011 with rcov 0.9.8