|
| 1 | +require "pg" |
| 2 | +require "singleton" |
| 3 | +require "pry" |
| 4 | +require "ostruct" |
| 5 | +require "active_support/inflector" |
| 6 | + |
| 7 | +class Connection |
| 8 | + include Singleton |
| 9 | + |
| 10 | + def connection |
| 11 | + @connection ||= PG::Connection.open(dbname: "hsnews_development") |
| 12 | + end |
| 13 | +end |
| 14 | + |
| 15 | +class ActiveRecord < OpenStruct |
| 16 | + |
| 17 | + def self.connection |
| 18 | + Connection.instance.connection |
| 19 | + end |
| 20 | + |
| 21 | + def self.table_name |
| 22 | + self.name.downcase.pluralize |
| 23 | + end |
| 24 | + |
| 25 | + def self.all |
| 26 | + result = self.connection.exec("SELECT * FROM #{self.table_name}") |
| 27 | + |
| 28 | + result.map {|tuple| self.new(tuple) } |
| 29 | + end |
| 30 | + |
| 31 | + def self.find(id) |
| 32 | + instance = nil |
| 33 | + result = self.connection.exec("SELECT * FROM #{self.table_name} |
| 34 | + WHERE id = #{id} limit 1") |
| 35 | + |
| 36 | + result.each do |tuple| |
| 37 | + instance = self.new(tuple) |
| 38 | + end |
| 39 | + |
| 40 | + raise "Not Record Found" if instance.nil? |
| 41 | + |
| 42 | + instance |
| 43 | + end |
| 44 | + |
| 45 | + #INSERT INTO users( |
| 46 | + #name, email, id) |
| 47 | + #VALUES ('Abraham kuri', '[email protected]', 1); |
| 48 | + def save |
| 49 | + sql_column_names = self.to_h.keys.join(", ") |
| 50 | + values = self.to_h.values.map {|value| "'#{value}'"}.join(', ') |
| 51 | + #=> ['Abraham', 'kurenn...'] |
| 52 | + |
| 53 | + self.class.connection.exec("INSERT INTO #{self.class.table_name} ( |
| 54 | + #{sql_column_names}) |
| 55 | + VALUES (#{values})") |
| 56 | + self |
| 57 | + end |
| 58 | + |
| 59 | + def update(attr = {}) |
| 60 | + |
| 61 | + sql_update = attr.map {|k, v| "#{k} = '#{v}'" }.join(", ") |
| 62 | + |
| 63 | + self.class.connection.exec("UPDATE #{self.class.table_name} |
| 64 | + SET #{sql_update} |
| 65 | + WHERE id = #{self.id}") |
| 66 | + self.class.find(self.id) |
| 67 | + end |
| 68 | + |
| 69 | + def destroy |
| 70 | + self.class.connection.exec("DELETE FROM #{self.class.table_name} |
| 71 | + WHERE id = #{self.id}") |
| 72 | + nil |
| 73 | + end |
| 74 | + |
| 75 | + def self.create(attrs = {}) |
| 76 | + instance = self.new(attrs) |
| 77 | + instance.save |
| 78 | + end |
| 79 | + |
| 80 | + def self.where(conditions = {}) |
| 81 | + sql_where = conditions.map {|k, v| "#{k} = '#{v}'" }.join(" AND ") |
| 82 | + |
| 83 | + result = self.connection.exec("SELECT * FROM #{self.table_name} |
| 84 | + WHERE #{sql_where}") |
| 85 | + |
| 86 | + result.map { |tuple| self.new(tuple) } |
| 87 | + end |
| 88 | + |
| 89 | + def self.has_many(*args) |
| 90 | + association_class = args[0].to_s.classify #=> "Link" |
| 91 | + association_method_name = args[0] |
| 92 | + foreign_key = "#{self.name.downcase}_id" |
| 93 | + |
| 94 | + define_method association_method_name do |
| 95 | + association_class.constantize.where(foreign_key => self.id) |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + def self.belongs_to(*args) |
| 100 | + association_model = args[0].to_s |
| 101 | + association_method_name = args[0] |
| 102 | + |
| 103 | + define_method association_method_name do |model=""| |
| 104 | + instance_variable_set("@#{association_model}", association_model.classify.constantize.find(self.user_id)) if instance_variable_get("@#{association_model}").nil? |
| 105 | + instance_variable_get("@#{association_model}") |
| 106 | + end |
| 107 | + |
| 108 | + define_method "#{association_method_name}=" do |model| |
| 109 | + instance_variable_set("@#{association_model}", model) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | +end |
| 114 | + |
| 115 | +class User < ActiveRecord |
| 116 | + has_many :links |
| 117 | +end |
| 118 | + |
| 119 | +class Link < ActiveRecord |
| 120 | + belongs_to :user |
| 121 | +end |
| 122 | + |
| 123 | +binding.pry |
| 124 | + |
| 125 | +User.all |
| 126 | +# => [#<User name: "">, #<User name: "">] |
| 127 | + |
| 128 | +puts "Hola" |
| 129 | + |
| 130 | + |
0 commit comments