-
Notifications
You must be signed in to change notification settings - Fork 1
/
ticket_pool.rb
47 lines (41 loc) · 856 Bytes
/
ticket_pool.rb
1
2
3
4
5
6
7
8
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
# handles persistance of the pool of active tickets
class TicketPool < Array
def find_by_number(number)
select{ |v| v.number == number }
end
def find_or_create_by_number(number)
find = find_by_number(number)
if find.empty?
self << ticket = Ticket.new(number)
return [ticket]
else
return find
end
end
def reset
self.delete_if { |x| true }
end
def reset!
reset
save
end
def save
file = File.new('tickets.txt','w')
file.write Marshal.dump(self)
file.close
return self
end
def load
if File.exist?('tickets.txt')
file = File.open('tickets.txt','r')
self.reset
loaded = Marshal.load file.read
loaded.each_index{ |i| self[i] = loaded[i] }
file.close
end
return self
end
def self.load
self.new.load
end
end