lennarb 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lennarb/version.rb +1 -1
  3. data/lib/lennarb.rb +51 -37
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9dfa4cf1e25284df8669a9fdd9faf45806575137b0b2a2cd131e4af205426176
4
- data.tar.gz: 879b617cb51177961a055e2dfd069437e9cae0c7e781fb50aad410c14b271dd8
3
+ metadata.gz: 0e395a91c3650883770feaf41c00a2be532f9ac0b3919a6ed26fcc4758ae3319
4
+ data.tar.gz: 25f09b0d53fb906eee6cd73197b970a01213a64ee4e8e0052e8bc73dca506d0b
5
5
  SHA512:
6
- metadata.gz: '087d53fc3627a7c61f028826f0af5ea8fc26e6fb02a2a461c54154852bbc9b71d9d65becf7faf5451e81612eeb9129b307002861aac49b3e85ecc1dab4e6a6de'
7
- data.tar.gz: 74d0156c558d8619e66b2daa5d5449216d064b38c511efb01cea802397efc2323b149e71cb1a3756af591c58b10510aef48dcc77ec40e20cbbae6f2a350a38a7
6
+ metadata.gz: 034171202b487ef2b9de9fd21995df2918f6e4da6427088c665644646f34e8d3d4e4238b8708de2fb89a3303fd9d3d3f334c8402ee66ba70fdb889f8be7eca00
7
+ data.tar.gz: 66308251d5eaf6361f8e4d464858a068dfeaa49051d2cfb1157e2ca52a21338639ba7af8ceb92bba15ca8bbed76124a493c30d58563b1c0403fc403c0946ad87
@@ -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.1'
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,13 @@ 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 post(path, &block) = add_route(path, :POST, block)
80
+ def put(path, &block) = add_route(path, :PUT, block)
81
+ def patch(path, &block) = add_route(path, :PATCH, block)
82
+ def delete(path, &block) = add_route(path, :DELETE, block)
83
+
84
+ private
82
85
 
83
86
  # Add a route
84
87
  #
@@ -88,39 +91,18 @@ class Lennarb
88
91
  #
89
92
  # @returns [void]
90
93
  #
91
- def __add_route__(path, http_method, block)
94
+ def add_route(path, http_method, block)
92
95
  parts = SplitPath[path]
93
96
  @root.add_route(parts, http_method, block)
94
97
  end
95
98
 
96
- # Base module for the application. The main purpose is to include the class methods
97
- # and call the Lennarb instance.
99
+ # Base class for Lennarb applications
98
100
  #
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)
101
+ class ApplicationBase < Lennarb
102
+ @routes = []
115
103
 
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
104
+ class << self
105
+ attr_reader :routes
124
106
 
125
107
  # Add a route
126
108
  #
@@ -129,11 +111,43 @@ class Lennarb
129
111
  #
130
112
  # @returns [void]
131
113
  #
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)
114
+ %i[get post put patch delete].each do |http_method|
115
+ define_method(http_method) do |path, &block|
116
+ routes << { method: http_method, path:, proc: block }
117
+ end
118
+ end
119
+ end
120
+
121
+ # Inherited hook
122
+ #
123
+ # @parameter [Class] subclass
124
+ #
125
+ # @returns [void]
126
+ #
127
+ def self.inherited(subclass)
128
+ super
129
+ subclass.instance_variable_set(:@routes, routes.dup)
130
+ end
131
+
132
+ # Initialize the application
133
+ #
134
+ # @returns [ApplicationBase]
135
+ #
136
+ def initialize
137
+ super
138
+ setup_routes
139
+ end
140
+
141
+ private
142
+
143
+ # Setup the routes
144
+ #
145
+ # @returns [void]
146
+ #
147
+ def setup_routes
148
+ self.class.routes.each do |route_info|
149
+ __send__(route_info[:method], route_info[:path], &route_info[:proc])
150
+ end
137
151
  end
138
152
  end
139
153
  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.1
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