Created
October 22, 2019 09:57
-
-
Save jonny-gates/989b978bd20fff5a1ec2e7eaacb6a62f 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
require_relative 'patient' | |
class Room | |
attr_accessor :id | |
class RoomCapacityError < Exception; end | |
# STATE | |
# capacity - integer | |
# clean - boolean | |
# patients - array | |
def initialize(attributes = {}) | |
@capacity = attributes[:capacity] | |
@patients = attributes[:patients] || [] | |
@clean = attributes[:clean] || true | |
@id = attributes[:id] | |
end | |
# BEHAVIOUR | |
def add_patient(patient) | |
if full? | |
raise RoomCapacityError, 'Room is full' | |
else | |
@patients << patient | |
patient.room = self | |
end | |
end | |
def full? | |
@patients.length == @capacity | |
end | |
end | |
room_1 = Room.new(capacity: 3, clean: false) | |
john = Patient.new(name: 'John') | |
ringo = Patient.new(name: 'ringo') | |
paul = Patient.new(name: 'paul') | |
room_1.add_patient(john) | |
room_1.add_patient(ringo) | |
begin | |
room_1.add_patient(paul) | |
rescue Room::RoomCapacityError | |
puts 'Code is ok, but Paul was not added' | |
end | |
# p room_1 | |
# how do I get? | |
# john.room | |
# john.room= | |
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' | |
patients = [] | |
csv_file = 'patients.csv' | |
# Tell our csv foreach method that the first row is headers | |
csv_options = { headers: :first_row, header_converters: :symbol } | |
CSV.foreach(csv_file, csv_options) do |row| | |
# Convert our row | |
row[:id] = row[:id].to_i # Convert column to Integer | |
row[:cured] = row[:cured] == "true" # Convert column to boolean | |
# Passing only the row object to our patient initialize method | |
patients << Patient.new(row) | |
end | |
p patients |
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_accessor :room, :id | |
# STATE | |
# name - string | |
# cured - boolean | |
def initialize(attributes = {}) | |
@name = attributes[:name] | |
@cured = attributes[:cured] || false | |
@blood_type = attributes[:blood_type] || 'A' | |
@id = attributes[:id] | |
end | |
end | |
# john = Patient.new(name: 'John', cured: false) | |
# ringo = Patient.new(name: 'Ringo', cured: true, blood_type: 'A') | |
# p john | |
# p ringo |
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' | |
require 'pry' | |
class PatientRepository | |
attr_reader :patients | |
def initialize(csv_file, room_repository) | |
@csv_file = csv_file | |
@patients = [] | |
@next_id = 1 | |
@room_repo = room_repository | |
load_csv | |
end | |
def add_patient(patient) | |
patient.id = @next_id | |
@next_id += 1 | |
@patients << patient | |
end | |
private | |
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 Integer | |
row[:cured] = row[:cured] == "true" # Convert column to boolean | |
# Create a patient | |
patient = Patient.new(row) | |
# Add the room to patient | |
# p row[:room_id] | |
# binding.pry | |
patient.room = @room_repo.find(row[:room_id].to_i) | |
# Add the patient to patients | |
@patients << patient | |
end | |
@next_id = @patients.empty? ? 1 : @patients.last.id + 1 | |
end | |
end | |
repo = PatientRepository.new('patients.csv', RoomRepository.new('rooms.csv')) | |
john = Patient.new(name: 'John') | |
repo.add_patient(john) | |
p repo.patients.first | |
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 '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 } | |
end | |
private | |
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 Integer | |
row[:capacity] = row[:capacity].to_i # Convert column to boolean | |
@rooms << Room.new(row) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment