-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputs_output.rb
73 lines (64 loc) · 2.23 KB
/
inputs_output.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module InputOutput
def write_file(taked_book, file_name)
json_file = JSON.generate(taked_book)
File.open(file_name, 'w')
File.write(file_name, json_file)
end
def read_file_book
return [] unless File.exist?('books.json')
file_name = 'books.json'
File.open(file_name)
take_data = File.read('books.json')
@taked_book = JSON.parse(take_data)
convert_book
end
def convert_book
@taked_book.each do |i|
@books.push(Book.new(i['title'], i['author']))
end
end
# Read peoples
def read_file_people
return [] unless File.exist?('people.json')
file_name = 'people.json'
File.open(file_name)
take_data = File.read('people.json')
@taked_people = JSON.parse(take_data)
convert_people
end
def convert_people
@taked_people.each do |i|
if i['type'] == 'Teacher'
@people.push(Teacher.new(i['specialization'], i['type'], i['age'], i['name'], i['id']))
else
@people.push(Students.new(i['classroom'], i['type'], i['age'], i['name'], i['id'],
parent_permission: i['parent_permission']))
end
end
end
# Read rental in file rentals
def read_file_rental
return [] unless File.exist?('rentals.json')
file_name = 'rentals.json'
File.open(file_name)
take_data = File.read('rentals.json')
@taked_rental = JSON.parse(take_data)
convert_rental
end
def convert_rental
@taked_rental.each do |i|
if i['person']['type'] == 'Student'
@rental.push(Rental.new(i['date'],
Students.new(i['person']['classroom'], i['person']['type'],
i['person']['age'], i['person']['name'],
i['person']['id'], i['person']['permission']),
Book.new(i['book']['title'], i['book']['author'])))
else
@rental.push(Rental.new(i['date'],
Teacher.new(i['person']['specialization'], i['person']['type'],
i['person']['age'], i['person']['name'], i['person']['id']),
Book.new(i['book']['title'], i['book']['author'])))
end
end
end
end