Skip to content

Instantly share code, notes, and snippets.

@arthur-littm
Created July 27, 2021 09:28
Show Gist options
  • Save arthur-littm/7be5bc2bfd873fd955f79c284086e81c to your computer and use it in GitHub Desktop.
Save arthur-littm/7be5bc2bfd873fd955f79c284086e81c to your computer and use it in GitHub Desktop.
class Patient
# data
# name
# cured
attr_accessor :room, :id
attr_reader :name
def initialize(attributes = {})
@id = attributes[:id] # string
@name = attributes[:name] # string
@cured = attributes[:cured] || false # boolean
@room = attributes[:room] # instance of Room
end
# behavior
def cure
@cured = true
end
end
# Testing
# arthur = Patient.new('Arthur', false)
arthur = Patient.new({cured: false, name: 'Arthur'})
mark = Patient.new({name: 'Mark'})
# lucy = Patient.new()
# p mark
# p arthur
# arthur.cure
# p arthur
id name cured
1 john false
2 arthur false
3 lucy true
require 'csv'
require_relative 'patient'
class PatientsRepository
# data
# all the patients
# csv
def initialize(csv_file)
@patients = [] # Array of patient instances
@csv_file = csv_file
@next_id = 1
# load csv
load_csv # next id will be 3 after this method
end
# patient will arrive without an ID
def add_patient(patient)
#
patient.id = @next_id
@next_id += 1
@patients << patient
end
# behavior
def load_csv
csv_options = { headers: :first_row, header_converters: :symbol }
CSV.foreach(@csv_file, csv_options) do |row|
# convert row before adding it
row[:id] = row[:id].to_i
row[:cured] = row[:cured] == 'true'
# ??? push patients inside of @patients
# @patients << Patient.new(row) # because row is a hash!!!
@patients << Patient.new({id: row[:id], name: row[:name], cured: row[:cured]})
@next_id = row[:id]
end
@next_id += 1
end
end
repo = PatientsRepository.new('patients.csv')
p repo
require_relative 'patient'
class Room
# data
# room number
# capacity
# patients
def initialize(attributes = {})
@capacity = attributes[:capacity] || 1 # Integer
@patients = attributes[:patients] || [] # Array of Patient instance
end
# behavior
def add_patient(patient)
unless full?
@patients << patient
patient.room = self
else
raise StandardError, "room is full!"
end
end
def full?
@capacity <= @patients.size
end
end
# Testing
room_1 = Room.new({capacity: 2})
arthur = Patient.new({cured: false, name: 'Arthur'})
paul = Patient.new({cured: false, name: 'paul'})
lucy = Patient.new({cured: false, name: 'lucy'})
room_1.add_patient(arthur)
room_1.add_patient(paul)
room_1.add_patient(lucy)
p room_1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment