Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created May 1, 2018 10:09
Show Gist options
  • Save rodloboz/9735f4ed56010262a6e3765b1f6f2985 to your computer and use it in GitHub Desktop.
Save rodloboz/9735f4ed56010262a6e3765b1f6f2985 to your computer and use it in GitHub Desktop.
Modeling Data
class Patient
attr_reader :name, :cured
attr_accessor :id, :room # attr_red + attr_writer
# STATE:
# - name (String)
# - cured (Boolean)
# STRATEGY 1
# def initialize(name, cured)
# @name = name
# @cured = cured
# end
# STRATEGY 2
# alternative/improved strategy
# works for many instance variables
def initialize(attributes = {})
@id = attributes[:id]
@name = attributes[:name]
@cured = attributes[:cured] || false # adds a default value
@blood_type = attributes[:blood_type]
# ...
end
# attr_reader (getter)
# def name
# @name
# end
# attr_writer (setter)
# def name=(name)
# @name = name
# end
def cure
@cured = true
end
end
# STRATEGY 1: order of the params matters
# john = Patient.new("John", false)
# STRATEGY 2: order is not important
john = Patient.new({name: "John", cured: false})
mary = Patient.new({cured: true, name: "Mary", blood_type: "O-"})
paul = Patient.new({name: "Paul"})
# curly braces are optional
# if the last argument is an hash
# (Rails uses this syntax everywhere)
susan = Patient.new(name: "Susan", blood_type: "A", cured: true)
# p john
# p mary
# p paul
# p susan.name
# susan.name=("Jane")
# p susan.name
require 'csv'
require_relative 'patient'
class PatientsRepository
def initialize(csv_file, room_repository)
@room_repository = room_repository
@csv_file = csv_file
@patients = []
@next_id = 1 # for auto-increment strategy
load_csv
end
def find(id)
# TODO: return a room with a specific id
end
def add(patient)
# auto-increment strategy
patient.id = @next_id
@patients << patient
@next_id += 1
end
private
def load_csv
csv_options = { headers: :first_row, header_converters: :symbol }
# go through each row of the csv file
@next_id = 0
CSV.foreach(@csv_file, csv_options) do |row|
# row is a hash like CSV object
row[:id] = row[:id].to_i # Convert column to Integer
row[:cured] = row[:cured] == "true" # Convert column to boolean
# fetch room from the rooms repo
# with the id row[:room_id]
room = @room_repository.find(row[:room_id])
patient.room = room
@patients << Patient.new(row)
@next_id = row[:id]
end
@next_id += 1
end
end
repo = PatientsRepository.new("patients.csv")
# john = Patient.new(name: "John")
# susan = Patient.new(name: "Susan")
# repo.add(john)
# repo.add(susan)
p repo
require_relative "patient"
class Room
class CapacityReachedError < Exception; end
attr_accessor :id
# STATE?
# - capacity (Fixnum)
# - patients (Array of Patient instances)
def initialize(attributes = {})
@capacity = attributes[:capacity]
@patients = attributes[:patients] || [] # set default
end
# BEHAVIOUR?
def full?
@capacity == @patients.length
end
def add_patient(patient)
if full?
# TODO: raise a custom error
fail CapacityReachedError, "The room is full"
else
@patients << patient
# inform patient of the room
patient.room = self # self = the room instance
end
end
end
room_1 = Room.new(capacity: 2)
puts "Is it full?"
p room_1.full?
john = Patient.new(name: "John")
room_1.add_patient(john) # Add the patient to the room
# Ask the patient what room he is in?
# p room_1
p john.room
susan = Patient.new(name: "Susan")
room_1.add_patient(susan)
begin
mary = Patient.new(name: "Mary")
room_1.add_patient(mary)
# keeps the program from crashing
rescue Room::CapacityReachedError
puts "Sorry, this room is already full!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment