forked from gglin/student-sinatra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
99 additions
and
70 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'sinatra', require: 'sinatra/base' | ||
gem 'sinatra', require: 'sinatra/base' | ||
gem 'sqlite3' |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,43 @@ | ||
require_relative '../concerns/persistable' | ||
require_relative '../concerns/findable' | ||
|
||
class Student | ||
extend Persistable::ClassMethods | ||
|
||
extend Persistable::ClassMethods | ||
include Persistable::InstanceMethods | ||
|
||
extend Findable::ClassMethods | ||
|
||
ATTRIBUTES = { | ||
:id => "INTEGER PRIMARY KEY", | ||
:name => "TEXT", | ||
:url => "TEXT", | ||
:img => "TEXT", | ||
:tagline => "TEXT", | ||
:bio => "TEXT", | ||
:treehouse_profile => "TEXT" | ||
:prof_pic => "TEXT", | ||
:twitter => "TEXT", | ||
:linkedin => "TEXT", | ||
:github => "TEXT", | ||
:blog => "TEXT", | ||
:quote => "TEXT" | ||
} | ||
|
||
def self.attributes | ||
ATTRIBUTES | ||
end | ||
|
||
def self.find_by(*args) | ||
args.flatten.each do |arg| | ||
define_singleton_method("find_by_#{arg}") do |value| | ||
result = self.database.execute "SELECT * FROM #{self.table_name} WHERE #{arg} = ?", value | ||
new_from_db(result.first) | ||
end | ||
end | ||
end | ||
|
||
find_by(self.attributes.keys) | ||
|
||
def self.find(id) | ||
self.find_by_id(id) | ||
end | ||
|
||
def self.all | ||
results = database.execute "SELECT * FROM students;" | ||
results.collect{|row| new_from_db(row)} | ||
end | ||
|
||
def self.attr_accessors | ||
self.attributes.keys.each do |k| | ||
attr_accessor k | ||
end | ||
end | ||
attr_accessors | ||
|
||
def self.database | ||
@@db ||= SQLite3::Database.new('student.db') | ||
end | ||
|
||
def persisted? | ||
self.id | ||
end | ||
|
||
def save | ||
persisted? ? update : insert | ||
end | ||
|
||
def attributes | ||
self.class.attributes.keys.collect do |attribute| | ||
self.send(attribute) | ||
end | ||
end | ||
|
||
def attributes_for_sql | ||
self.attributes[1..-1] | ||
end | ||
find_by(self.attributes.keys) | ||
|
||
def question_marks_for_sql | ||
("?," * attributes_for_sql.size)[0..-2] | ||
def self.db | ||
@@db ||= SQLite3::Database.new('student.db') | ||
end | ||
|
||
private | ||
def insert | ||
self.class.database.execute "INSERT INTO #{self.class.table_name} (#{self.class.column_names_for_insert}) VALUES (#{self.question_marks_for_sql})", self.attributes_for_sql | ||
self.id = self.class.database.last_insert_row_id | ||
end | ||
|
||
def update | ||
self.class.database.execute "UPDATE #{self.class.table_name} SET #{self.class.attributes_for_update} WHERE id = ?", [attributes_for_sql, self.id].flatten | ||
end | ||
end |
Binary file not shown.
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