Created
July 23, 2014 00:42
-
-
Save mayfer/5050c61f0fcacb6cbb0b to your computer and use it in GitHub Desktop.
ORM breakout
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
class ORM | |
def save | |
table_name = self.class | |
sql_columns = self.instance_variables.map do |i| | |
i.slice(1, i.length) | |
end.join(', ') | |
sql_values = self.instance_variables.map do |i| | |
'"' << self.instance_variable_get("#{i}") << '"' | |
end.join(", ") | |
sql_command = "INSERT INTO #{table_name} | |
(#{sql_columns}) | |
VALUES | |
(#{sql_values})" | |
end | |
end | |
class User < ORM | |
attr_accessor :name, :email, :phone | |
def initialize(name, email, phone, gender) | |
@id = "5" | |
@name = name | |
@email = email | |
@phone = phone | |
@gender = gender | |
end | |
end | |
class Dog < ORM | |
attr_accessor :num_legs, :personality_type, :bark_pitch | |
end | |
dog = Dog.new | |
dog.num_legs = 3 | |
dog.personality_type = 'evil' | |
dog.bark_pitch = 'high pitched' | |
puts dog.save | |
murat = User.new("murat", "[email protected]", "1234234", "doesnt matter in this day and age") | |
puts murat.save | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment