From c0e4d36d809150120247f55b65e8ed7acd5f63cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arist=C3=B3teles=20Coutinho?= Date: Thu, 8 Feb 2024 09:30:05 -0300 Subject: [PATCH 1/4] test: ensure subclass has http_methods --- test/lib/test_lennarb.rb | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/test/lib/test_lennarb.rb b/test/lib/test_lennarb.rb index ea19e11..a60ab44 100644 --- a/test/lib/test_lennarb.rb +++ b/test/lib/test_lennarb.rb @@ -29,15 +29,31 @@ def test_initialize_with_block assert_respond_to app, :call end - def test_extends_application_base - extend Lennarb::ApplicationBase - - assert_respond_to self, :call - assert_respond_to self, :get - assert_respond_to self, :post - assert_respond_to self, :put - assert_respond_to self, :patch - assert_respond_to self, :delete + class TestApp < Lennarb::ApplicationBase + get '/' do |_req, res| + res.html('GET Response') + end + + post '/' do |_req, res| + res.html('POST Response') + end + end + + def test_subclass_has_http_methods + assert_respond_to TestApp, :get + assert_respond_to TestApp, :post + assert_respond_to TestApp, :put + assert_respond_to TestApp, :delete + assert_respond_to TestApp, :patch + end + + def test_routes_are_defined + refute_empty TestApp.routes + assert_equal 2, TestApp.routes.count + assert_equal :get, TestApp.routes.first[:method] + assert_equal '/', TestApp.routes.first[:path] + assert_equal :post, TestApp.routes.last[:method] + assert_equal '/', TestApp.routes.last[:path] end def app From 0d7b9a62a0a92c723d72ec24c3e14e40ffaa410f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arist=C3=B3teles=20Coutinho?= Date: Thu, 8 Feb 2024 09:30:55 -0300 Subject: [PATCH 2/4] refactor: chenge Application behavior --- lib/lennarb.rb | 88 +++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 37 deletions(-) diff --git a/lib/lennarb.rb b/lib/lennarb.rb index 96a6d1e..5aa043b 100644 --- a/lib/lennarb.rb +++ b/lib/lennarb.rb @@ -27,6 +27,7 @@ class LennarbError < StandardError; end # attr_reader :root + @routes = [] # Initialize the application # # @yield { ... } The application @@ -74,11 +75,13 @@ def call(env) # # @returns [void] # - def get(path, &block) = __add_route__(path, :GET, block) - def post(path, &block) = __add_route__(path, :POST, block) - def put(path, &block) = __add_route__(path, :PUT, block) - def patch(path, &block) = __add_route__(path, :PATCH, block) - def delete(path, &block) = __add_route__(path, :DELETE, block) + def get(path, &block) = add_route(path, :GET, block) + def post(path, &block) = add_route(path, :POST, block) + def put(path, &block) = add_route(path, :PUT, block) + def patch(path, &block) = add_route(path, :PATCH, block) + def delete(path, &block) = add_route(path, :DELETE, block) + + private # Add a route # @@ -88,39 +91,18 @@ def delete(path, &block) = __add_route__(path, :DELETE, block) # # @returns [void] # - def __add_route__(path, http_method, block) + def add_route(path, http_method, block) parts = SplitPath[path] @root.add_route(parts, http_method, block) end - # Base module for the application. The main purpose is to include the class methods - # and call the Lennarb instance. + # Base class for Lennarb applications # - module ApplicationBase - # Include the class methods - # - # @parameter [Class] base - # - # @returns [void] - # - def self.included(base) = base.extend(ClassMethods) - - # Call the Lennarb instance - # - # @parameter [Hash] env - # - # @returns [Array] - # - def call(env) = self.class.lennarb_instance.call(env) + class ApplicationBase < Lennarb + @routes = [] - # Class methods - # - module ClassMethods - # Get the Lennarb instance - # - # @returns [Lennarb] - # - def lennarb_instance = @lennarb_instance ||= Lennarb.new + class << self + attr_reader :routes # Add a route # @@ -129,11 +111,43 @@ def lennarb_instance = @lennarb_instance ||= Lennarb.new # # @returns [void] # - def get(path, &block) = lennarb_instance.__add_route__(path, :GET, block) - def put(path, &block) = lennarb_instance.__add_route__(path, :PUT, block) - def post(path, &block) = lennarb_instance.__add_route__(path, :POST, block) - def patch(path, &block) = lennarb_instance.__add_route__(path, :PATCH, block) - def delete(path, &block) = lennarb_instance.__add_route__(path, :DELETE, block) + %i[get post put patch delete].each do |http_method| + define_method(http_method) do |path, &block| + routes << { method: http_method, path:, proc: block } + end + end + end + + # Inherited hook + # + # @parameter [Class] subclass + # + # @returns [void] + # + def self.inherited(subclass) + super + subclass.instance_variable_set(:@routes, routes.dup) + end + + # Initialize the application + # + # @returns [ApplicationBase] + # + def initialize + super + setup_routes + end + + private + + # Setup the routes + # + # @returns [void] + # + def setup_routes + self.class.routes.each do |route_info| + __send__(route_info[:method], route_info[:path], &route_info[:proc]) + end end end end From 5a5b51748b64c424ea482565f6b48cfa349df03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arist=C3=B3teles=20Coutinho?= Date: Thu, 8 Feb 2024 09:31:39 -0300 Subject: [PATCH 3/4] chore: bump version to 0.4.1 --- lib/lennarb/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lennarb/version.rb b/lib/lennarb/version.rb index a63ade6..76a86d8 100644 --- a/lib/lennarb/version.rb +++ b/lib/lennarb/version.rb @@ -4,7 +4,7 @@ # Copyright, 2023-2024, by Aristóteles Coutinho. class Lennarb - VERSION = '0.4.0' + VERSION = '0.4.1' public_constant :VERSION end From 90bf76709986b5920f301edd7814df3afe8592fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arist=C3=B3teles=20Coutinho?= Date: Thu, 8 Feb 2024 09:37:05 -0300 Subject: [PATCH 4/4] docs: update changelog.md --- changelog.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/changelog.md b/changelog.md index fa608b5..810a95a 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,31 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.4.1] - 2024-08-02 + +### Change + +- Change behavior of `Lennarb::ApplicationBase` class to be the base class of the `Lennarb` class. Now, the `Lennarb` class is a subclass of `Lennarb::ApplicationBase` class. + +That permits to create a new application with the `Lennarb::ApplicationBase` class and use http methods to create the routes. Ex. + +```rb +# app.rb + +require 'lennarb' + +class MyApp < Lennarb::ApplicationBase + get '/hello' do |req, res| + res.html('Hello World') + end +end +``` + +### Removed + +- Remove `Lennarb::Application` module from the project. Now, the `Lennarb` class is the main class of the project. + + ## [0.4.0] - 2024-07-02 ### Added