Class: Lennarb::Base
- Inherits:
-
Object
- Object
- Lennarb::Base
- Defined in:
- lib/lennarb/base.rb
Overview
Base class for mounting applications with middleware support. This class serves as a proxy for mounting Lennarb::App instances, providing a lightweight router to dispatch requests to the appropriate app.
Constant Summary collapse
- AlreadyInitializedError =
This error is raised whenever the app is initialized more than once.
Class.new(StandardError)
Instance Attribute Summary collapse
-
#env ⇒ Lennarb::Environment
The current environment.
-
#mounted_apps ⇒ Hash
readonly
Get the mounted applications.
-
#root ⇒ Pathname
The root directory of the application.
Class Method Summary collapse
-
.config(*envs) {|config| ... } ⇒ Lennarb::Config
Define the app’s configuration at class level.
-
.middleware {|middleware| ... } ⇒ Lennarb::MiddlewareStack
Define the app’s middleware stack at class level.
-
.mount(component, at: nil) ⇒ void
Mount a component at the given path.
-
.mounted_apps ⇒ Hash
Store for mounted applications at class level.
Instance Method Summary collapse
-
#app ⇒ #call
The Rack app with all middlewares and mounted applications.
-
#call(env) ⇒ Array(Integer, Hash, #each)
Call the app - main Rack entry point.
-
#config(*envs) {|config| ... } ⇒ Lennarb::Config
Define the app’s configuration.
-
#freeze! ⇒ void
Freeze the app.
-
#initialize {|self| ... } ⇒ Base
constructor
Initialize a new Base instance.
-
#initialize! ⇒ self
Initialize the app.
-
#initialized? ⇒ Boolean
Check if the app is initialized.
-
#middleware {|middleware| ... } ⇒ Lennarb::MiddlewareStack
Define the app’s middleware stack.
-
#mount(component, at: nil) ⇒ void
Mount a component at the given path (instance method).
Constructor Details
#initialize {|self| ... } ⇒ Base
Initialize a new Base instance.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/lennarb/base.rb', line 127 def initialize(&block) @initialized = false @mounted_apps = {} @middleware = nil self.root = Pathname.pwd @env = Environment.new(compute_env) # Initialize mounted applications from class definition self.class.mounted_apps.each do |path, app_class| @mounted_apps[path] = app_class end instance_eval(&block) if block_given? end |
Instance Attribute Details
#env ⇒ Lennarb::Environment
The current environment.
111 112 113 |
# File 'lib/lennarb/base.rb', line 111 def env @env end |
#mounted_apps ⇒ Hash (readonly)
Get the mounted applications.
121 122 123 |
# File 'lib/lennarb/base.rb', line 121 def mounted_apps @mounted_apps end |
#root ⇒ Pathname
The root directory of the application.
116 117 118 |
# File 'lib/lennarb/base.rb', line 116 def root @root end |
Class Method Details
.config(*envs) {|config| ... } ⇒ Lennarb::Config
Define the app’s configuration at class level
70 71 72 73 74 |
# File 'lib/lennarb/base.rb', line 70 def config(*envs, &block) @config ||= Config.new @config.instance_eval(&block) if block_given? @config end |
.middleware {|middleware| ... } ⇒ Lennarb::MiddlewareStack
Define the app’s middleware stack at class level. This allows defining middleware that will be applied before routing to any mounted applications.
49 50 51 52 53 |
# File 'lib/lennarb/base.rb', line 49 def middleware(&block) @middleware ||= MiddlewareStack.new @middleware.instance_eval(&block) if block_given? @middleware end |
.mount(component, at: nil) ⇒ void
This method returns an undefined value.
Mount a component at the given path.
87 88 89 90 91 92 93 94 |
# File 'lib/lennarb/base.rb', line 87 def mount(component, at: nil) if component.is_a?(Class) && component < Lennarb::App path = normalize_mount_path(at || "/") mounted_apps[path] = component else raise ArgumentError, "Component must be a Lennarb::App subclass" end end |
.mounted_apps ⇒ Hash
Store for mounted applications at class level
32 33 34 |
# File 'lib/lennarb/base.rb', line 32 def mounted_apps @mounted_apps ||= {} end |
Instance Method Details
#app ⇒ #call
The Rack app with all middlewares and mounted applications. This builds a middleware stack around the URL map of mounted applications.
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/lennarb/base.rb', line 216 def app @app ||= begin url_map = build_url_map stack = middleware.to_a Rack::Builder.app do stack.each { |middleware, args, block| use(middleware, *args, &block) } run url_map end end end |
#call(env) ⇒ Array(Integer, Hash, #each)
Call the app - main Rack entry point. This method is called by Rack when a request is received.
235 236 237 238 239 240 241 |
# File 'lib/lennarb/base.rb', line 235 def call(env) # Store reference to the current app in the env env[RACK_LENNA_APP] = self # Call the app with middlewares app.call(env) end |
#config(*envs) {|config| ... } ⇒ Lennarb::Config
Define the app’s configuration.
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/lennarb/base.rb', line 166 def config(*envs, &block) @config ||= Config.new write = block_given? && (envs.map(&:to_sym).include?(env.to_sym) || envs.empty?) @config.instance_eval(&block) if write @config end |
#freeze! ⇒ void
This method returns an undefined value.
Freeze the app.
207 208 209 |
# File 'lib/lennarb/base.rb', line 207 def freeze! app.freeze end |
#initialize! ⇒ self
Initialize the app.
188 189 190 191 192 |
# File 'lib/lennarb/base.rb', line 188 def initialize! raise AlreadyInitializedError if initialized? @initialized = true self end |
#initialized? ⇒ Boolean
Check if the app is initialized.
180 181 182 |
# File 'lib/lennarb/base.rb', line 180 def initialized? @initialized end |
#middleware {|middleware| ... } ⇒ Lennarb::MiddlewareStack
Define the app’s middleware stack. Middleware defined here will be applied to all requests before they are routed to mounted applications.
155 156 157 158 159 |
# File 'lib/lennarb/base.rb', line 155 def middleware(&block) @middleware ||= MiddlewareStack.new @middleware.instance_eval(&block) if block_given? @middleware end |
#mount(component, at: nil) ⇒ void
This method returns an undefined value.
Mount a component at the given path (instance method)
253 254 255 256 257 258 259 260 |
# File 'lib/lennarb/base.rb', line 253 def mount(component, at: nil) if component.is_a?(Class) && component < Lennarb::App path = normalize_mount_path(at || "/") @mounted_apps[path] = component else raise ArgumentError, "Component must be a Lennarb::App subclass" end end |