Last active
May 4, 2021 11:41
-
-
Save Oceantidote/5322ecd768f8890a37fd67e3001e0819 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CREATING INSTANCES OF REPOSITORIES | |
# make room repo | |
# make doctor repo | |
# pass the room repo to patient repo | |
# make appointment repo | |
# make controllers | |
# make router (which takes all the controllers as arguments) | |
# Router.new(controllers.....).run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Patient | |
attr_reader :name, :blood_type | |
attr_accessor :cured, :room, :id | |
def initialize(attr = {}) | |
@id = attr[:id] | |
@name = attr[:name] | |
@blood_type = attr[:blood_type] || "A" | |
@cured = attr[:cured] || false | |
@room = attr[:room] | |
end | |
def cure | |
@cured = true | |
end | |
def infect | |
@cured = false | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require_relative 'patient' | |
require_relative 'room_repository' | |
class PatientRepository | |
def initialize(csv_file, room_repository) | |
@room_repository = room_repository | |
@next_id = 1 | |
@patients = [] | |
@csv_file = csv_file | |
load_csv | |
end | |
def load_csv | |
csv_options = { headers: :first_row, header_converters: :symbol } | |
CSV.foreach(@csv_file, csv_options) do |row| | |
row[:id] = row[:id].to_i | |
row[:cured] = row[:cured] == "true" | |
room = @room_repository.find(row[:room_id].to_i) | |
row[:room] = room | |
patient = Patient.new(row) | |
@patients << Patient.new(row) | |
room.add_patient(patient) | |
end | |
@next_id = @patients.empty? ? 1 : @patients.last.id + 1 | |
end | |
def add_patient(patient) | |
patient.id = @next_id | |
@patients << patient | |
@next_id += 1 | |
save_csv | |
end | |
def save_csv | |
CSV.open(@csv_file, "wb") do |csv| | |
csv << ["id","name","cured","blood_type"] | |
@patients.each do |patient| | |
csv << [patient.id, patient.name, patient.cured, patient.blood_type, patient.room.id] | |
end | |
end | |
end | |
end | |
# room_repo = RoomRepository.new('rooms.csv') | |
# p patients_repo = PatientsRepository.new('patients.csv', room_repo) | |
# bob = Patient.new(name: 'bob') | |
# repo.add_patient(bob) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | name | cured | blood_type | room_id | |
---|---|---|---|---|---|
1 | paul | true | a | 1 | |
2 | john | false | b | 2 | |
3 | bob | false | a | 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'patient' | |
class Room | |
attr_reader :capacity | |
attr_accessor :id | |
def initialize(attr = {}) | |
@id = attr[:id] | |
@capacity = attr[:capacity] || 1 | |
@patients = attr[:patients] || [] | |
end | |
def full? | |
@patients.length == @capacity | |
end | |
def add_patient(patient) | |
raise StandardError, "Room is full!" if full? | |
patient.room = self | |
@patients << patient | |
# if full? | |
# puts "Room full, the patient wasn't added" | |
# else | |
# patient.room = self | |
# @patients << patient | |
# end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require_relative 'room' | |
class RoomRepository | |
def initialize(csv_file) | |
@next_id = 1 | |
@rooms = [] | |
@csv_file = csv_file | |
load_csv | |
end | |
def find(id) | |
@rooms.find { |room| room.id == id } | |
end | |
def load_csv | |
csv_options = { headers: :first_row, header_converters: :symbol } | |
CSV.foreach(@csv_file, csv_options) do |row| | |
row[:id] = row[:id].to_i | |
row[:capacity] = row[:capacity].to_i | |
@rooms << Room.new(row) | |
end | |
@next_id = @rooms.empty? ? 1 : @rooms.last.id + 1 | |
end | |
def save_csv | |
CSV.open(@csv_file, "wb") do |csv| | |
csv << ["id","capacity"] | |
@rooms.each do |room| | |
csv << [room.id, room.capacity] | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | capacity | |
---|---|---|
1 | 2 | |
2 | 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment