Rebuilding Rails Chapter 1

Rebuilding Rails Chapter 1

Build your own framework on RUBY!

Let's Start Rebuilding rails

Why Rebuild Rails?

Knowing the deepest level of any software lets us master that specific programming structure. We will be building a system in a Rails-like Framework, which will we'll call RULERs (like Ruby on Rails). Ruler is much simpler than the latest version of Rails.

Getting setup

We'll need:

  • Ruby 2.4.1
  • a text editor
  • a command line or terminal
  • Git
  • Bundler
  • POSTGRES || SQLite || MYSQL (your wish)

This edition of Rebuilding rails uses Ruby 2.4.1, "bundler", "~> 1.17" & "rake", "~> 10.0". You shouldn't need these exact versions. You can try adding your own version which you wish to use.

I will provide a complete flow from starting to how it works!

Chapter one:

First step is to create a new, empty gem such as:

bundle gem rulers
Creating gem 'rulers'...
Code of conduct enabled in config
      create  rulers/Gemfile
      create  rulers/lib/rulers.rb
      create  rulers/lib/rulers/version.rb
      create  rulers/rulers.gemspec
      create  rulers/Rakefile
      create  rulers/README.md
      create  rulers/bin/console
      create  rulers/bin/setup
      create  rulers/.gitignore
      create  rulers/CODE_OF_CONDUCT.md

In this, Rulers is a gem (which is a library), and it declares its dependencies are in rulers.gemspec. You can customize the rulers.gemspec with your name, description, and summary as you wish.

The file looks like this:

lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "rulers/version"

Gem::Specification.new do |gem|
  gem.name          = "rulers"
  gem.version       = Rulers::VERSION
  gem.authors       = ["subasskhatrii"]
  gem.email         = ["subass.khatrii@gmail.com"]
  gem.summary       = %q{A RACK-based Web Framework}
  gem.description   = %q{A  RACK-based Web Framework, but with extra awesome features.}
  gem.homepage      = ""

  gem.require_paths = ["lib"]
end

Make sure to replace the "TODO" in the descriptions as the "gem build" does not work properly and looks bad to users. We need to add dependencies at the bottom which looks like:

    # NOTE: Rack is a gem to interface your framework to a Ruby application server such as Mongrel, Thin, Lighttpd, Passenger, WEBrick, or Unicorn.
    # An application server is a special type of web server that runs server applications.
    # In the production environment we would run a web server like Apache or Nginx in front of application servers.
    # Important NOTE: Rack is how Ruby turns HTTP requests into code running on your server.
    gem.add_runtime_dependency "rack"

    gem.add_development_dependency "rspec"
    gem.add_runtime_dependency "rest-client"
    gem.add_development_dependency "bundler", "~> 1.17"
    gem.add_development_dependency "rake", "~> 10.0"

After adding the dependencies, the rulers.gemspec looks like this:

    lib = File.expand_path("../lib", __FILE__)
    $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
    require "rulers/version"

    Gem::Specification.new do |gem|
    gem.name          = "rulers"
    gem.version       = Rulers::VERSION
    gem.authors       = ["subasskhatrii"]
    gem.email         = ["subass.khatrii@gmail.com"]
    gem.summary       = %q{A RACK-based Web Framework}
    gem.description   = %q{A  RACK-based Web Framework, but with extra awesome features.}
    gem.homepage      = ""

    # NOTE: Rack is a gem to interface your framework to a Ruby application server such as Mongrel, Thin, Lighttpd, Passenger, WEBrick, or Unicorn.
    # An application server is a special type of web server that runs server applications.
    # In the production environment we would run a web server like Apache or Nginx in front of application servers.
    # Important NOTE: Rack is how Ruby turns HTTP requests into code running on your server.
    gem.add_runtime_dependency "rack"

    gem.require_paths = ["lib"]
    gem.add_development_dependency "rspec"
    gem.add_runtime_dependency "rest-client"

    gem.add_development_dependency "bundler", "~> 1.17"
    gem.add_development_dependency "rake", "~> 10.0"
    end

Now Let's build our gem and install it.

    > gem build rulers.gemspec
    > gem install rulers-0.1.0.gem

Now we will use our gem from the development directory with a Bundler trick.

Let's create directories and subdirectories that we will require with the flow.

    > mkdir project_rebuilding_rails
    > cd project_rebuilding_rails
    > git init
    Initialized empty Git repository in src/
    project_rebuilding_rails/.git/
    > mkdir config
    > mkdir app

Now we will be adding a gemfile to our root directory

    # project_rebuilding_rails/Gemfile
    source :rubygems
    gem 'rulers' # our gem

Now run bundle install to create a Gemfile.lock and make sure all dependencies are available.

We'll build from a trivial rack application. Create a config.ru file:

    run proc {
  [200, {'Content_Type' => 'text/html'},
  ["Hello, Nepal!"]]
}
# NOTE: Rack's "run" means "call that object for each request".

In this proc, it returns success (200) and "Hello, Nepal!" along with an HTTP header to make our browser display HTML content.

We can start it by adding rackup -p 3001 on our terminal and then this points to the browser localhost:3001.

image.png

Chapter Two coming soon!