Skip to content

Instantly share code, notes, and snippets.

@ikasamt
Created July 6, 2020 05:57
Show Gist options
  • Save ikasamt/8a801469b7c006986deea51d1730f781 to your computer and use it in GitHub Desktop.
Save ikasamt/8a801469b7c006986deea51d1730f781 to your computer and use it in GitHub Desktop.
simple-rails-like-framework.rb
## rails/database.rb
class Database
def connection()
return mysql.Connection()
end
end
## rails/test_databae.rb
class Database
def connection()
return sqlite3.Connection()
end
end
## rails/controller.rb
module Rails
class BaseController
def call(action_name)
eval("self.#{action_name}")
end
end
end
## rails/model.rb
module Rails
class BaseModel
def self.first
conn = Rails::Database.connection()
row = conn.execute("SELECT * FROM users ORDER BY id DESC LIMIT 1")
instance = bind(row)
return instance
end
end
end
## application/models/user.rb
class User < BaseModel
def self.bind(row)
instance = self()
instance.name = rows["name"]
instance.email = rows["email"]
instance.age = rows["age"]
return instance
end
end
## application/controllers/main.rb
class MainController < BaseController
def hello
me = User.first
json_response(value)
end
end
## application/init.rb
if ENV("test")
require "rails/test_databae.rb"
end
while do
regexp("/:controller_name/:action_name", url.path)
controller = eval("#{controller_name}Controller.new()")
http.responese(controller.call(action_name))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment