Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created July 23, 2014 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayfer/5050c61f0fcacb6cbb0b to your computer and use it in GitHub Desktop.
Save mayfer/5050c61f0fcacb6cbb0b to your computer and use it in GitHub Desktop.
ORM breakout
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", "a@b.c", "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