generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.rb
110 lines (100 loc) · 3.02 KB
/
data.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require './book'
require 'json'
module Data
def save_book(list)
if list == []
puts 'No Book data'
else
file_path = IO.sysopen('./book.json', 'w+')
list_add = IO.new(file_path)
list.each do |data|
hash = { title: data.title, author: data.author }
json = JSON.generate(hash)
list_add.puts(json)
end
end
end
def start_file
File.write('./rental.json', []) unless File.exist?('./rental.json')
end
def save_rental(list)
if list == []
puts 'No rental data'
else
rental_list = JSON.parse(File.read('./rental.json'))
list.each do |rental|
rental_list.push({
ID: rental.person.find_id,
date: rental.date,
book: { title: rental.book.title, author: rental.book.author },
person: { age: rental.person.age, name: rental.person.name }
})
end
File.write('./rental.json', JSON.generate(rental_list).to_s)
end
end
def save_person(list)
if list == []
puts 'No persom data'
else
file_path = IO.sysopen('./person.json', 'w+')
list_add = IO.new(file_path)
list.each do |data|
if data.instance_of?(Student)
hash = { age: data.age, name: data.name }
json = JSON.generate(hash)
list_add.puts(json)
elsif data.instance_of?(Teacher)
hash = { age: data.age, special: data.specialization, name: data.name }
json = JSON.generate(hash)
list_add.puts(json)
end
end
end
end
def retrieve
book = []
unless File.zero?('./book.json')
file = File.open('./book.json')
file_data = file.readlines.map(&:chomp)
file_data.each do |data|
parse_data = JSON.parse(data)
make_book = Book.new(parse_data['title'], parse_data['author'])
book.push(make_book)
end
end
book
end
def retrieve_person
person = []
unless File.zero?('./person.json')
file = File.open('./person.json')
file_data = file.readlines.map(&:chomp)
file_data.each do |data|
parse_data = JSON.parse(data)
make_person = if parse_data.include?('special')
Teacher.new(parse_data['age'], parse_data['special'], parse_data['name'])
else
Student.new(parse_data['age'], nil, parse_data['name'], parent_permission: true)
end
person.push(make_person)
end
person
end
person
end
def retrieve_rental(id)
rentals = []
unless File.zero?('./rental.json')
list = JSON.parse(File.read('./rental.json'))
list.each do |rental|
book = rental['book']
person = rental['person']
next unless id == rental['ID']
rentals.push(Rental.new(rental['date'], Book.new(book['title'], book['author']),
Person.new(person['age'], person['name'])))
end
end
rentals
end
end