Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Doctor
- attr_reader :id
- attr_accessor :name, :age, :specialty
- def initialize(attributes={})
- @id = attributes[:id]
- @name = attributes[:name]
- @age = attributes[:age]
- @specialty = attributes[:specialty]
- end
- def save
- @id.nil? ? insert : update
- end
- def destroy
- DB.execute('DELETE FROM doctors WHERE id = ?', @id)
- end
- def self.find(id)
- DB.results_as_hash = true
- row = DB.execute('SELECT * FROM doctors WHERE id = ?', id).first
- Doctor.new(name: row['name'], id: row['id'] , age: row['age'], specialty: row['specialty'])
- end
- def self.all
- DB.results_as_hash = true
- rows = DB.execute('SELECT * FROM doctors')
- result = []
- rows.each do |row|
- result << Doctor.new(name: row['name'], id: row['id'] , age: row['age'], specialty: row['specialty'])
- end
- result
- end
- private
- def insert
- DB.execute("INSERT INTO doctors (name, age, specialty) VALUES (?,?,?)", @name, @age, @specialty)
- @id = DB.last_insert_row_id
- end
- def update
- DB.execute("UPDATE doctors SET name=?, age=?, specialty=? WHERE id=?", @name, @age, @specialty, @id)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement