Created
February 2, 2018 10:24
-
-
Save nmajor/db982148348fdbfdb52a9eea24dc691c 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 'sqlite3' | |
DB = SQLite3::Database.new('test.db') | |
class Doctor | |
attr_reader :id | |
def initialize(attributes = {}) | |
@id = attributes[:id] | |
@name = attributes[:name] | |
@age = attributes[:age] | |
@specialty = attributes[:specialty] | |
end | |
def self.all | |
rows = DB.execute('SELECT * FROM doctors'); | |
end | |
def is_new? | |
@id.nil? | |
end | |
def new? | |
is_new? | |
end | |
def has_been_saved? | |
!is_new? | |
end | |
def save | |
query = "INSERT INTO doctors (name, age, specialty) VALUES ('#{@name}', #{@age}, '#{@specialty}')" | |
DB.execute(query); | |
@id = DB.last_insert_row_id | |
end | |
end | |
quinn = Doctor.new(name: 'Harley Quinn', age: 35) | |
p quinn | |
p quinn.is_new? | |
quinn.save | |
p quinn | |
p quinn.is_new? | |
quinn.age = quinn.age + 1; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment