Skip to content

Instantly share code, notes, and snippets.

@luizfonseca
Created July 18, 2017 15:54
Show Gist options
  • Save luizfonseca/11da92e735407ddba9cb1aa221916285 to your computer and use it in GitHub Desktop.
Save luizfonseca/11da92e735407ddba9cb1aa221916285 to your computer and use it in GitHub Desktop.
Patient + PatientRepository Examples for Food Delivery
class Patient
attr_accessor :room, :id
attr_reader :name, :cured
# STATES?
# - @name
# - @cured
# attr_reader :cured
def initialize(options = {})
@id = options[:id]
@name = options[:name] || ""
@cured = options[:cured] || false
end
def cured?
return @cured
end
end
# TODO: rename to your MODEL repository, they almost
require "csv"
require_relative "patient" # => Dealing with a model? Require the model here
class PatientRepository
# This method initializes with a path
# to a csv file (see `app.rb`)
def initialize(csv_file_path)
# Empty list on initializing
@patients = []
@csv_file_path = csv_file_path
# We read the CSV, and update the
# @patients list at the beginning!
load_csv
end
# Returning all the patients, on an Array format
def all
return @patients
end
# Find a patient based on his ID!
def find(patient_id)
@patients.select { |patient| patient.id == patient_id }
end
# Add a patient to the Array/List of Patients
# And write to the csv afterwards
def add_patient(patient)
patient.id = @next_id
@patients << patient
write_to_csv
end
# Delete Patient
def delete(patient_index)
@patients.delete_at(patient_index)
write_to_csv
end
private
# Common instruction: it updates the CSV everytime
# we change @patients
def write_to_csv
CSV.open(@csv_file_path, 'wb') do |csv|
csv << %w(id name cured)
@patients.each do |patient|
# UPDATE: we add new fields to the initialize
# now that we have more of them
# See line 19, we update it as well
csv << [patient.id, patient.name, patient.cured]
end
end
get_next_id
end
def load_csv
csv_options = { headers: :first_row, header_converters: :symbol }
CSV.foreach(@csv_file_path, csv_options) do |row|
# if row[:cured] == "true"
# true
# else
# false
# end
# row => { id: "", name: "", cured: ""}
# IF YOU NEED TO CONVERT FIELDS, DO THEM BELOW!!
row[:id] = row[:id].to_i # Convert column to Fixnum
row[:cured] = row[:cured] == "true" # Convert column to boolean
@patients << Patient.new(row)
end
get_next_id
end
def get_next_id
# If patients empty, the ID is 1
# If not, it's last patient id + 1
@next_id = @patients.empty? ? 1 : @patients.last.id + 1
end
end
require_relative "patient"
require_relative "patient_repository"
# Instantiating our repository,
# so we know where to store Patients
csv_file = File.join(__dir__, 'patients.csv')
repo = PatientRepository.new(csv_file)
# New instances of patient
luiz = Patient.new(name: "Luiz")
ronalson = Patient.new(name: "Ronalson")
# Add the patient to the CSV/Repo
repo.add_patient(luiz)
repo.add_patient(ronalson)
@ronalson
Copy link

@ patient_repository.rb

31  def find(patient_id)
32    @patients.select { |patient| patient.id == patient_id }
33  end

.select method is returning an Array, this method should use .find instead.

@luizfonseca
Copy link
Author

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment