Created
October 13, 2015 18:29
-
-
Save mayfer/e9f9f3efde65100d9c15 to your computer and use it in GitHub Desktop.
Intro to ORMs
This file contains hidden or 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 'pg' | |
CONN = PG::Connection.new({ | |
host: 'localhost', | |
user: 'murat', | |
password: '', | |
dbname: 'lighthouse_2015_10' | |
}) | |
# ORM: object relational model | |
class ORM | |
def initialize(object_hash) | |
object_hash.each do |column, value| | |
instance_variable_set("@#{column}", value) | |
self.class.send(:attr_accessor, column.to_sym) | |
end | |
end | |
def instance_variables_hash | |
Hash[instance_variables.map { |name| [name[1..-1], instance_variable_get(name)] } ] | |
end | |
def to_s | |
instance_variables_hash.inspect | |
end | |
def self.convert_to_objects(results) | |
objects = [] | |
results.each do |result| | |
object = self.new(result) | |
objects << object | |
end | |
return objects | |
end | |
def get_table_name | |
return "#{self.class.name.downcase}s" | |
end | |
def self.get_table_name | |
return "#{self.name.downcase}s" | |
end | |
def self.all | |
results = CONN.exec_params("SELECT * FROM #{get_table_name}") | |
return convert_to_objects(results) | |
end | |
def self.search(column, name) | |
results = CONN.exec_params("SELECT * FROM #{get_table_name} WHERE #{column} ILIKE '%#{name}%'") | |
return convert_to_objects(results) | |
end | |
def self.find_by_id(id) | |
results = CONN.exec_params("SELECT * FROM #{get_table_name} WHERE id=#{id}") | |
objects = convert_to_objects(results) | |
if objects.length > 1 | |
raise Exception, "Find by ID returned too many results" | |
else | |
return objects[0] | |
end | |
end | |
def save | |
if @id | |
# update | |
columns = instance_variables_hash.keys.join(", ") | |
values = instance_variables_hash.values.map { |v| "'#{v}'" }.join(", ") | |
statements = instance_variables_hash.select do |key, val| | |
key != "id" | |
end.map do |key, val| | |
"#{key}='#{val}'" | |
end | |
puts "UPDATING" | |
query = "UPDATE #{get_table_name} SET #{statements.join(', ')} WHERE id=#{@id}" | |
puts query | |
else | |
columns = instance_variables_hash.keys.join(", ") | |
values = instance_variables_hash.values.map { |v| "'#{v}'" }.join(", ") | |
puts "INSERTING" | |
query = "INSERT INTO #{get_table_name} (#{columns}) VALUES (#{values})" | |
puts query | |
end | |
CONN.exec_params(query) | |
end | |
end | |
class Book < ORM | |
end | |
class Better_table < ORM | |
end | |
existing_table = Better_table.find_by_id(1) | |
puts existing_table | |
existing_table.color = "orange" | |
existing_table.save | |
existing_table = Better_table.find_by_id(1) | |
puts existing_table | |
This file contains hidden or 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 'pg' | |
CONN = PG::Connection.new({ | |
host: 'localhost', | |
user: 'murat', | |
password: '', | |
dbname: 'lighthouse_2015_10' | |
}) | |
class Book | |
def initialize(book_hash) | |
book_hash.each do |column, value| | |
instance_variable_set("@#{column}", value) | |
end | |
end | |
def to_s | |
return "#{@title}, by #{@author_id}" | |
end | |
def self.convert_to_objects(results) | |
books = [] | |
results.each do |result| | |
book = Book.new(result) | |
books << book | |
end | |
return books | |
end | |
def self.all | |
results = CONN.exec_params("SELECT * FROM books") | |
return convert_to_objects(results) | |
end | |
def search(name) | |
results = CONN.exec_params("SELECT * FROM books WHERE title ILIKE '%#{name}%'") | |
return convert_to_objects(results) | |
end | |
end | |
#Book.all.each do |book| | |
# puts book | |
#end | |
#puts "-----" | |
class Author | |
def initialize(author_hash) | |
author_hash.each do |column, value| | |
instance_variable_set("@#{column}", value) | |
end | |
end | |
def to_s | |
return "#{@first_name} #{@last_name}" | |
end | |
def self.convert_to_objects(results) | |
authors = [] | |
results.each do |result| | |
author = Author.new(result) | |
authors << author | |
end | |
return authors | |
end | |
def self.all | |
results = CONN.exec_params("SELECT * FROM authors") | |
return convert_to_objects(results) | |
end | |
def search(name) | |
results = CONN.exec_params("SELECT * FROM authors WHERE first_name ILIKE '%#{name}%' OR last_name ILIKE '%#{name}%'") | |
return convert_to_objects(results) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment