Created
July 10, 2018 13:24
-
-
Save nmajor/267201fb1fbb15193d80c20b584d32ec 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 'byebug' | |
require_relative 'patient' | |
require_relative 'room' | |
harry = Patient.new(name: 'Harry Potter', age: 28) | |
ron = Patient.new(name: 'Ron Weasley', age: 28) | |
hermione = Patient.new(name: 'Hermione Granger', age: 28) | |
draco = Patient.new(name: 'Draco Malfoy', age: 28) | |
hogwarts_room = Room.new(capacity: 3, decorative_theme: 'Hogwarts') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
my_little_pony_room = Room.new(capacity: 3, decorative_theme: 'My Little Pony') | |
harry.room = hogwarts_room | |
hogwarts_room.patients << harry | |
p harry | |
p hogwarts_room | |
hogwarts_room.add_patient(harry) | |
# hogwarts_room.add_patient(ron) | |
# hogwarts_room.add_patient(hermione) | |
# hogwarts_room.add_patient(draco) | |
# byebug |
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_reader :name | |
attr_accessor :id, :room | |
def initialize(attributes = {}) | |
@id = attributes[:id] | |
@name = attributes[:name] | |
@cured = attributes[:cured] || false | |
@age = attributes[:age] | |
end | |
def cure! | |
@cured = true | |
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
class PatientRepository | |
def initialize(csv_file) # 'patients.csv' | |
@csv_file = csv_file | |
@patients = [] | |
@next_id = 1 | |
load_csv | |
end | |
def find(id) | |
@patients.select{|patient| patient.id == id }.first | |
end | |
def add_patient(patient) | |
patient.id = @next_id | |
@next_id += 1 | |
@patients << patient | |
save_csv | |
end | |
def load_csv | |
csv_options = { headers: :first_row, header_converters: :symbol } | |
CSV.foreach(@csv_file, csv_options) do |row| | |
@next_id += 1 | |
patient = Patient.new({ id: row['id'], name: row['name'], cured: row['cured'] }) | |
room = @room_repository.find(row['room_id']) | |
room.add_patient(patient) | |
@patients << patient | |
end | |
end | |
def save_csv | |
@patients.each do |patient| | |
# save to csv | |
end | |
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
id | name | cured | room_id | |
---|---|---|---|---|
1 | harry | true | 1 | |
2 | ron | false | 2 | |
4 | hermione | false | 1 | |
5 | draco | false | 1 |
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 Room | |
attr_reader :decorative_theme | |
attr_accessor :id, :patients | |
def initialize(attributes = {}) | |
@id = attributes[:id] | |
@capacity = attributes[:capacity] || 0 | |
@patients = [] | |
@decorative_theme = attributes[:decorative_theme] | |
end | |
def full? | |
@patients.size == @capacity | |
end | |
def add_patient(patient) | |
p self.decorative_theme # Hogwarts | |
# fail(Exception, "Room is full!") if full? | |
@patients << patient | |
patient.room = self | |
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
id | capacity | decorative_theme | |
---|---|---|---|
1 | 3 | Hogwarts | |
2 | 3 | My Little Pony |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment