Skip to content

Commit

Permalink
Merge pull request #42 from aristotelesbr/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
aristotelesbr authored Feb 8, 2024
2 parents dfcc13a + 90bf767 commit 72b9a19
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 47 deletions.
25 changes: 25 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 51 additions & 37 deletions lib/lennarb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LennarbError < StandardError; end
#
attr_reader :root

@routes = []
# Initialize the application
#
# @yield { ... } The application
Expand Down Expand Up @@ -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
#
Expand All @@ -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
#
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/lennarb/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 25 additions & 9 deletions test/lib/test_lennarb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 72b9a19

Please sign in to comment.