lennarb 0.4.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9dfa4cf1e25284df8669a9fdd9faf45806575137b0b2a2cd131e4af205426176
4
- data.tar.gz: 879b617cb51177961a055e2dfd069437e9cae0c7e781fb50aad410c14b271dd8
3
+ metadata.gz: d8f83fbde3862f89138c06960e7e10d92febf1b2568ec1ac7d5f414e56eb7861
4
+ data.tar.gz: 2ae1191814fc395c684f7d3b52a3d16d41cdb722d61ebd1c18937ab7e2eeff00
5
5
  SHA512:
6
- metadata.gz: '087d53fc3627a7c61f028826f0af5ea8fc26e6fb02a2a461c54154852bbc9b71d9d65becf7faf5451e81612eeb9129b307002861aac49b3e85ecc1dab4e6a6de'
7
- data.tar.gz: 74d0156c558d8619e66b2daa5d5449216d064b38c511efb01cea802397efc2323b149e71cb1a3756af591c58b10510aef48dcc77ec40e20cbbae6f2a350a38a7
6
+ metadata.gz: 9bf21b6aeaaafc3b1c1f795ae356883ad6c4581506527f6b92b2a10b823770e6d5a8249dc7ebacc0776e0d5a8b4d10a818dcb05ac22ec5cb48e84c7f027d9925
7
+ data.tar.gz: 96a9067abdf1f6062da9cac71fd79df3a7da8d01a19d662419c9f7d9498438b57299074a5ac2ac86b44271f263e0a52c69d8f48e69c7843177191a653d68e508
data/changelog.md CHANGED
@@ -5,6 +5,31 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.4.1] - 2024-08-02
9
+
10
+ ### Change
11
+
12
+ - 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.
13
+
14
+ That permits to create a new application with the `Lennarb::ApplicationBase` class and use http methods to create the routes. Ex.
15
+
16
+ ```rb
17
+ # app.rb
18
+
19
+ require 'lennarb'
20
+
21
+ class MyApp < Lennarb::ApplicationBase
22
+ get '/hello' do |req, res|
23
+ res.html('Hello World')
24
+ end
25
+ end
26
+ ```
27
+
28
+ ### Removed
29
+
30
+ - Remove `Lennarb::Application` module from the project. Now, the `Lennarb` class is the main class of the project.
31
+
32
+
8
33
  ## [0.4.0] - 2024-07-02
9
34
 
10
35
  ### Added
@@ -80,7 +80,7 @@ class Lennarb
80
80
  def write(str)
81
81
  str = str.to_s
82
82
  @length += str.bytesize
83
- @headers[CONTENT_LENGTH] ||= @length.to_s
83
+ @headers[CONTENT_LENGTH] = @length.to_s
84
84
  @body << str
85
85
  end
86
86
 
@@ -4,7 +4,7 @@
4
4
  # Copyright, 2023-2024, by Aristóteles Coutinho.
5
5
 
6
6
  class Lennarb
7
- VERSION = '0.4.0'
7
+ VERSION = '0.4.2'
8
8
 
9
9
  public_constant :VERSION
10
10
  end
data/lib/lennarb.rb CHANGED
@@ -27,6 +27,7 @@ class Lennarb
27
27
  #
28
28
  attr_reader :root
29
29
 
30
+ @routes = []
30
31
  # Initialize the application
31
32
  #
32
33
  # @yield { ... } The application
@@ -74,11 +75,15 @@ class Lennarb
74
75
  #
75
76
  # @returns [void]
76
77
  #
77
- def get(path, &block) = __add_route__(path, :GET, block)
78
- def post(path, &block) = __add_route__(path, :POST, block)
79
- def put(path, &block) = __add_route__(path, :PUT, block)
80
- def patch(path, &block) = __add_route__(path, :PATCH, block)
81
- def delete(path, &block) = __add_route__(path, :DELETE, block)
78
+ def get(path, &block) = add_route(path, :GET, block)
79
+ def put(path, &block) = add_route(path, :PUT, block)
80
+ def post(path, &block) = add_route(path, :POST, block)
81
+ def head(path, &block) = add_route(path, :HEAD, block)
82
+ def patch(path, &block) = add_route(path, :PATCH, block)
83
+ def delete(path, &block) = add_route(path, :DELETE, block)
84
+ def options(path, &block) = add_route(path, :OPTIONS, block)
85
+
86
+ private
82
87
 
83
88
  # Add a route
84
89
  #
@@ -88,39 +93,18 @@ class Lennarb
88
93
  #
89
94
  # @returns [void]
90
95
  #
91
- def __add_route__(path, http_method, block)
96
+ def add_route(path, http_method, block)
92
97
  parts = SplitPath[path]
93
98
  @root.add_route(parts, http_method, block)
94
99
  end
95
100
 
96
- # Base module for the application. The main purpose is to include the class methods
97
- # and call the Lennarb instance.
101
+ # Base class for Lennarb applications
98
102
  #
99
- module ApplicationBase
100
- # Include the class methods
101
- #
102
- # @parameter [Class] base
103
- #
104
- # @returns [void]
105
- #
106
- def self.included(base) = base.extend(ClassMethods)
107
-
108
- # Call the Lennarb instance
109
- #
110
- # @parameter [Hash] env
111
- #
112
- # @returns [Array]
113
- #
114
- def call(env) = self.class.lennarb_instance.call(env)
103
+ class ApplicationBase < Lennarb
104
+ @routes = []
115
105
 
116
- # Class methods
117
- #
118
- module ClassMethods
119
- # Get the Lennarb instance
120
- #
121
- # @returns [Lennarb]
122
- #
123
- def lennarb_instance = @lennarb_instance ||= Lennarb.new
106
+ class << self
107
+ attr_reader :routes
124
108
 
125
109
  # Add a route
126
110
  #
@@ -129,11 +113,43 @@ class Lennarb
129
113
  #
130
114
  # @returns [void]
131
115
  #
132
- def get(path, &block) = lennarb_instance.__add_route__(path, :GET, block)
133
- def put(path, &block) = lennarb_instance.__add_route__(path, :PUT, block)
134
- def post(path, &block) = lennarb_instance.__add_route__(path, :POST, block)
135
- def patch(path, &block) = lennarb_instance.__add_route__(path, :PATCH, block)
136
- def delete(path, &block) = lennarb_instance.__add_route__(path, :DELETE, block)
116
+ %i[get post put patch delete].each do |http_method|
117
+ define_method(http_method) do |path, &block|
118
+ routes << { method: http_method, path:, proc: block }
119
+ end
120
+ end
121
+ end
122
+
123
+ # Inherited hook
124
+ #
125
+ # @parameter [Class] subclass
126
+ #
127
+ # @returns [void]
128
+ #
129
+ def self.inherited(subclass)
130
+ super
131
+ subclass.instance_variable_set(:@routes, routes.dup)
132
+ end
133
+
134
+ # Initialize the application
135
+ #
136
+ # @returns [ApplicationBase]
137
+ #
138
+ def initialize
139
+ super
140
+ setup_routes
141
+ end
142
+
143
+ private
144
+
145
+ # Setup the routes
146
+ #
147
+ # @returns [void]
148
+ #
149
+ def setup_routes
150
+ self.class.routes.each do |route_info|
151
+ __send__(route_info[:method], route_info[:path], &route_info[:proc])
152
+ end
137
153
  end
138
154
  end
139
155
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lennarb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aristóteles Coutinho
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-07 00:00:00.000000000 Z
11
+ date: 2024-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize