-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstored.rb
More file actions
48 lines (46 loc) · 1.47 KB
/
stored.rb
File metadata and controls
48 lines (46 loc) · 1.47 KB
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
48
def list_all_stored_books
if File.exist?('book.json') && !File.zero?('book.json')
bookfile = File.open('book.json')
bookjson = bookfile.read
JSON.parse(bookjson).map do |bk|
example = Book.new(bk['title'], bk['author'])
@book.push(example)
end
bookfile.close
else
File.new('book.json', 'w')
end
end
def list_all_stored_people
if File.exist?('people.json') && !File.zero?('people.json')
peoplefile = File.open('people.json')
peoplejson = peoplefile.read
JSON.parse(peoplejson).map do |pep|
if pep['classroom']
@people.push(Student.new(pep['classroom'], pep['age'], pep['name'],
pep['parents_permission'], id: pep['id']))
else
@people.push(Teacher.new(pep['specialization'], pep['age'], pep['name']))
end
end
peoplefile.close
else
File.new('people.json', 'w')
end
end
def list_all_stored_rentals
if File.exist?('rental.json') && !File.zero?('rental.json')
rentalfile = File.open('rental.json')
rentaljson = rentalfile.read
JSON.parse(rentaljson).map do |ren|
book = Book.new(ren['book']['title'], ren['book']['author'])
person = Student.new('11a', ren['person']['age'], ren['person']['name'], ren['person']['parents_permission'],
id: ren['person']['id'])
item = Rental.new(ren['date'], book, person)
@rental.push(item)
end
rentalfile.close
else
File.new('rental.json', 'w')
end
end