Created
May 21, 2014 18:00
-
-
Save mayfer/b9f4626a44f779062960 to your computer and use it in GitHub Desktop.
Generic ORM example
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' | |
class Orm | |
@@conn = PG.connect( | |
dbname: 'dfq35t7uirbums', | |
port: 5432, | |
user: 'bmdjwluxchptuq', | |
host: 'ec2-54-204-41-178.compute-1.amazonaws.com', | |
password: 'aEH-cKdr2zoXYUAjI8Xjma5eXK' | |
) | |
def initialize(*args) | |
end | |
def self.all | |
table = "#{self.name.downcase}s" | |
puts "table name is #{table}" | |
instances = [] | |
@@conn.exec("SELECT * FROM #{table}").each do |row| | |
instance = self.new(*row.values) | |
instances << instance | |
end | |
instances | |
end | |
def save | |
end | |
def destroy | |
end | |
end | |
class Author < Orm | |
def initialize(id, last_name, first_name) | |
@first_name = first_name | |
@last_name = last_name | |
@id = id | |
end | |
def to_s | |
"#{@first_name} #{@last_name}" | |
end | |
end | |
authors = Author.all | |
authors.each do |author| | |
puts author | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment