Skip to content

Instantly share code, notes, and snippets.

@arthur-littm
Created October 17, 2017 09:35
Show Gist options
  • Save arthur-littm/f05427661f49b93d38e85f1c12dc8e86 to your computer and use it in GitHub Desktop.
Save arthur-littm/f05427661f49b93d38e85f1c12dc8e86 to your computer and use it in GitHub Desktop.
class Patient
attr_accessor :room, :id
attr_reader :name, :cured
def initialize(attributes = {})
@id = attributes[:id]
@room = attributes[:room] # Instance of Room
@name = attributes[:name]
@cured = attributes[:cured] || false
end
def cure
@cured = true
end
end
# --- TESTING ---
# john = Patient.new(cured: true, name: 'john')
# alex = Patient.new(name: "alex")
require 'csv'
class PatientsRepo
attr_reader :patients
def initialize(csv_file, rooms_repo)
@rooms_repo = rooms_repo
@csv_file = csv_file
@patients = []
@next_id = 1
load_csv
end
def add(patient)
patient.id = @next_id
@patients << patient
@next_id += 1
end
def load_csv
csv_options = { headers: :first_row, header_converters: :symbol }
CSV.foreach(@csv_file, csv_options) do |row|
# We have an id and we want an instance
# room = room_repo.find(row[:room_id])
row[:id] = row[:id].to_i # Convert column to Fixnum
row[:cured] = row[:cured] == "true" # Convert column to boolean
# { name: "Alex", id: 1, cured: false}
p row[:room_id]
room = @rooms_repo.find(row[:room_id].to_i)
patient = Patient.new(row)
room.add_patient(patient)
@patients << patient
@next_id = row[:id]
end
@next_id += 1
end
end
require_relative 'patient'
require_relative 'rooms_repo'
rooms_repo = RoomsRepo.new('rooms.csv')
repo = PatientsRepo.new('patients.csv', rooms_repo)
john = Patient.new(cured: true, name: 'john')
repo.add(john)
paul = repo.patients.first
p paul.room
class Room
attr_accessor :id
# STATE
def initialize(attributes = {})
@id = attributes[:id]
@capacity = attributes[:capacity] # Fixnum
@patients = attributes[:patients] || [] # Array of Patient instances
end
def full?
@capacity == @patients.length
end
def add_patient(patient)
if full?
fail Exception, "The room is full"
else
@patients << patient
patient.room = self
end
end
end
# --- TESTING ---
require_relative 'patient'
alex = Patient.new(name: "alex")
sandrine = Patient.new(name: "sandrine")
edward = Patient.new(name: "edward")
room_1 = Room.new(capacity: 2)
# begin
# room_1.add_patient(alex)
# room_1.add_patient(sandrine)
# room_1.add_patient(edward)
# rescue Exception
# puts "SOmething went wrong"
# end
p room_1
# p alex.room
require 'csv'
require_relative 'room'
class RoomsRepo
def initialize(csv_file)
@csv_file = csv_file
@rooms = []
@next_id = 1
load_csv
end
def find(id)
@rooms.find { |room| id == room.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 # Convert column to Fixnum
row[:capacity] = row[:capacity].to_i # Convert column to Fixnum
@rooms << Room.new(row)
@next_id = row[:id]
end
@next_id += 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment