Skip to content

Instantly share code, notes, and snippets.

@kenrett
Last active March 1, 2017 12:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kenrett/30de10fb57fe2219dd2f274bbdf48072 to your computer and use it in GitHub Desktop.
Save kenrett/30de10fb57fe2219dd2f274bbdf48072 to your computer and use it in GitHub Desktop.
Hospital MVC Example
class Hospital
attr_reader :patients, :name
def initialize(name, patients = [])
@name = name
@patients = patients
end
def find_patient_by_name(name)
patients.find { |patient| patient.name == name }
end
def patient_count
patients.count
end
end
class HospitalController
def initialize(hospital)
@hospital = hospital
end
def run(command, *options)
case command
when "hospital" then show_hospital
when "patient" then show_patient(options.first)
end
end
private
attr_reader :hospital
def show_hospital
HospitalView.show(hospital)
end
def show_patient(search_name)
patient = hospital.find_patient_by_name(search_name)
if patient
PatientView.show(patient)
else
printer("Could not find a patient with the name #{search_name}.")
end
end
end
module HospitalView
def self.show(hospital)
"#{hospital.name}\n#{hospital.patient_count} patients admitted."
end
end
class Patient
attr_reader :date_of_birth, :medical_records_number, :name
def initialize(args = {})
@date_of_birth = args[:date_of_birth]
@medical_records_number = args[:medical_records_number]
@name = args[:name]
end
alias_method :mrn, :medical_records_number
alias_method :dob, :date_of_birth
end
module PatientView
def self.show(patient)
display_albert
puts "^"
puts "|"
puts "|"
printer("Medical # | Name | Birthdate")
printer("#{patient.mrn} - #{patient.name} (DOB: #{format_dob(patient.dob)})")
end
private
def self.format_dob(date)
date.strftime("%m/%d/%Y")
end
end
require_relative "patient"
require_relative "patient_view"
require_relative "hospital"
require_relative "hospital_view"
require_relative "hospital_controller"
require_relative "view_helper"
require 'date'
if ARGV.any?
patients = [Patient.new(name: "Albert", date_of_birth: Date.new(1879, 3, 14), medical_records_number: "12345")]
hospital = Hospital.new("St. Charles Hospital", patients)
controller = HospitalController.new(hospital)
puts controller.run(*ARGV)
end
def printer(string)
string.split(//).each do |letter|
print letter
sleep(0.02)
end
puts ""
end
def display_albert
string = <<-EOD
/ ( ( ( ( ( ( ( ( ( ( ( (\\_
_/ `. `. `. `. `. `. `. `. `. `. `. `. `.\\_
/ ) ) ) ) ) ) ) ) ) ) ) ) ) ) \\_
/ .' .' .' .' .' .' .' .' .' .' .' .' .' .' .' \\_
| ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( \\_
|. `. `. `. `. `. `. `__`__`__`__`. `. `. `. `. `. `\\_
\\) ) ) ) )__)__)/ \\_)__) ) ) ) ) )\\_
|.' .' .' .'/ \\.' .' .' .' .' .'\\_
| ( ( ( / \\ ( ( ( ( ( ( \\
|`. `. `. | \\ `. `. `. `. `. `.\\
| ) ) )| \\ ) ) ) ) ) )|
|.' .' .' | \\' .' .' .' .' .' |
| ( ( (| ______ _____ |( ( ( ( ( (|
|`. `. `. | /______) (_____\\ | `. `. `. `. `. |
| ) ) )|// \\\_ \\ ) ) ) ) \/
\\' .' .' | _____ _____ |'__' .' .' ./
|( ( (| ( [O] ) ( [O] ) |/ \\( ( ((
| `. `. | \\___/ \\___/ \\ |`. `. `\\
\\_ ) )| , / | ) ) \\
\\ .' .\ | . / .' .' .' \\
\\_ ( | ( ) |\_/ ( ( ( ( )
\\ `.| \___/ |. `. `. `. `__/
\\__| | ) ) ) __/
| _______ |' .' .__/
| _/////\\\\\\\\\\____ _/.\\____/_____
| ///////\\\\\\\\\\\\\\\ / |.|\\,%\\,%\\,%\\_______
\ |///////\\\\\\\\\|\\\ _/ |.|\\%,\\%,\\%,\\%,\\%,\\%,\\____
\ \__________/ / /. |%\\,%\\,%\\,%\\,%\\,%\\,%\\,%\\
__\\ / /. /%,\\%,\\%,\\%,\\%,\\%,\\%,\\%,\\
_/%\\,\\_ __/ /. /%\\,%\\,%\\,%\\,%\\,%\\,%\\,%\\,%
_________/%\\,%\\,%\\_ ___/ _/. /\\,%\\,%\\,%\\,%\\,%\\,%\\,%\\,%\\,
_/%,\\%,\\%,\\%,\\%,\\%/XX\\_______/ _/. ./,\\%,\\%,\\%,\\%,\\%,\\%,\\%,\\%,\\%
__/,%\\,%\\,%\\,%\\,%\\,%/XXXXX\\_ __/. ./%\\,%\\,%\\,%\\,%\\,%\\,%\\,%\\,%\\,%\\
/%,\\%,\\%,\\%,\\%,\\%,\\%/XXXXXXXX\\ /. . ./%,\\%,\\%,\\%,\\%,\\%,\\%,\\%,\\%,\\%,\\
EOD
puts string
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment