Created
July 18, 2017 15:54
-
-
Save luizfonseca/11da92e735407ddba9cb1aa221916285 to your computer and use it in GitHub Desktop.
Patient + PatientRepository Examples for Food Delivery
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 | |
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 |
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
# 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 |
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" | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Done!