Skip to content

Instantly share code, notes, and snippets.

@forresty
Created July 18, 2017 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forresty/644df77428178edf248dd3deb295b515 to your computer and use it in GitHub Desktop.
Save forresty/644df77428178edf248dd3deb295b515 to your computer and use it in GitHub Desktop.
class Patient
attr_accessor :id, :room
attr_reader :name
def initialize(attributes = {})
@id = attributes[:id]
@name = attributes[:name]
@cured = attributes[:cured] || false
attributes[:room].add_patient(self)
end
def cured?
@cured
end
end
# p Patient.new(name: 'forrest')
require "csv"
require_relative 'patient'
require_relative 'room_repository'
class PatientRepository
def initialize(csv_file, room_repository)
@csv_file = csv_file
@room_repository = room_repository
@patients = []
load_csv
end
def find(patient_id)
@patients.find { |patient| patient.id == patient_id }
end
def find_all_by_room_id(room_id)
@patients.select { |patient| patient.room_id == room_id.to_i }
end
def add_patient(new_patient)
new_patient.id = @next_id
@next_id += 1
@patients << new_patient
save_csv
end
private
def save_csv
csv_options = { col_sep: ',', force_quotes: false }
CSV.open(@csv_file, 'wb', csv_options) do |csv|
csv << %w{ id name cured room_id }
@patients.each do |patient|
csv << [patient.id, patient.name, patient.cured?, patient.room.id]
end
end
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 # Convert column to Fixnum
row[:cured] = row[:cured] == "true" # Convert column to boolean
row[:room] = @room_repository.find(row[:room_id])
@patients << Patient.new(row)
end
@next_id = @patients.empty? ? 1 : @patients.last.id + 1
end
end
# room_repo = RoomRepository.new('rooms.csv')
# patient_repo = PatientRepository.new('patients.csv', room_repo)
# room = room_repo.find(3)
# p1 = Patient.new(name: 'allen', room: room)
# patient_repo.add_patient(p1)
# p2 = Patient.new(name: 'Dan')
# repo.add_patient(p2)
# repo.save_csv
# p patient_repo.find(2)
id name cured room_id
1 forrest false 3
2 Dan false 3
3 allen false 3
require_relative 'patient'
class HospitalError < StandardError
end
class InsufficientCapacityError < HospitalError
end
class Room
attr_accessor :id
attr_reader :capacity, :patients
def initialize(attributes = {})
@id = attributes[:id]
@capacity = attributes[:capacity]
@patients = []
end
def full?
@patients.length == @capacity
end
def add_patient(new_patient)
raise InsufficientCapacityError if full?
@patients << new_patient
new_patient.room = self
end
end
# p1 = Patient.new(name: 'forrest')
# room = Room.new(capacity: 1)
# room.add_patient(p1)
# # p room
# # p p1.room
# puts p1.room == room
# p2 = Patient.new(name: 'arafat')
# room.add_patient(p2)
# p room
require "csv"
require_relative 'room'
class RoomRepository
def initialize(csv_file)
@csv_file = csv_file
@rooms = []
load_csv
end
def find(room_id)
@rooms.find { |room| room.id == room_id.to_i }
end
def add_room(new_room)
new_room.id = @next_id
@next_id += 1
@rooms << new_room
save_csv
end
private
def save_csv
csv_options = { col_sep: ',', force_quotes: false }
CSV.open(@csv_file, 'wb', csv_options) do |csv|
csv << %w{ id capacity }
@rooms.each do |room|
csv << [room.id, room.capacity]
end
end
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
end
repo = RoomRepository.new('rooms.csv')
room = repo.find(3)
# require_relative 'patient_repository'
# patient_repo = PatientRepository.new('patients.csv', repo)
# room.patients = patient_repo.find_all_by_room_id(room.id)
p room
# room1 = Room.new(capacity: 10)
# repo.add_room(room1)
# room2 = Room.new(capacity: 20)
# repo.add_room(room2)
# repo.save_csv
# p repo.find(3)
id capacity
1 1
2 2
3 10
4 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment