Class: Lennarb::App
- Inherits:
-
Object
- Object
- Lennarb::App
- Defined in:
- lib/lennarb/app.rb
Overview
Lite implementation of app.
Direct Known Subclasses
Constant Summary collapse
- AlreadyInitializedError =
This error is raised whenever the app is initialized more than once.
Class.new(StandardError)
Instance Attribute Summary collapse
-
#env ⇒ Object
The current environment.
-
#root ⇒ Object
The root app directory of the app.
Instance Method Summary collapse
-
#app ⇒ Object
The Rack app.
-
#call(env) ⇒ Object
Call the app.
-
#config(*envs) ⇒ Object
Define the app’s configuration.
-
#controllers ⇒ Object
(also: #mounted_apps)
Store mounted app’s.
-
#freeze! ⇒ Object
Freeze the app.
-
#initialize ⇒ App
constructor
A new instance of App.
-
#initialize! ⇒ Object
Initialize the app.
-
#initialized? ⇒ Boolean
Check if the app is initialized.
-
#middleware ⇒ Object
Define the app’s middleware stack.
-
#mount(*controllers) ⇒ Object
Mount an app at a specific path.
-
#routes ⇒ Object
Define the app’s route.
Constructor Details
Instance Attribute Details
#env ⇒ Object
The current environment. Defaults to “development”. It can be set using the following environment variables:
-
‘LENNA_ENV`
-
‘APP_ENV`
-
‘RACK_ENV`
23 24 25 |
# File 'lib/lennarb/app.rb', line 23 def env @env end |
Instance Method Details
#app ⇒ Object
The Rack app.
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/lennarb/app.rb', line 123 def app @app ||= begin request_handler = RequestHandler.new(self) stack = middleware.to_a Rack::Builder.app do stack.each { |middleware, args, block| use(middleware, *args, &block) } run request_handler end end end |
#call(env) ⇒ Object
Call the app.
@param env
179 180 181 182 |
# File 'lib/lennarb/app.rb', line 179 def call(env) env[RACK_LENNA_APP] = self Dir.chdir(root) { return app.call(env) } end |
#config(*envs) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/lennarb/app.rb', line 100 def config(*envs, &) @config ||= Config.new write = block_given? && (envs.map(&:to_sym).include?(env.to_sym) || envs.empty?) @config.instance_eval(&) if write @config end |
#controllers ⇒ Object Also known as: mounted_apps
Store mounted app’s
139 140 141 |
# File 'lib/lennarb/app.rb', line 139 def controllers @controllers ||= [] end |
#freeze! ⇒ Object
Freeze the app.
170 171 172 173 |
# File 'lib/lennarb/app.rb', line 170 def freeze! app.freeze routes.freeze end |
#initialize! ⇒ Object
Initialize the app.
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/lennarb/app.rb', line 154 def initialize! raise AlreadyInitializedError if initialized? if controllers.any? controllers.each do routes.store.merge!(it.routes.store) end end @initialized = true end |
#initialized? ⇒ Boolean
Check if the app is initialized.
148 |
# File 'lib/lennarb/app.rb', line 148 def initialized? = @initialized |
#middleware ⇒ Object
Define the app’s middleware stack. See Middleware::Stack for more details.
75 76 77 78 79 |
# File 'lib/lennarb/app.rb', line 75 def middleware(&) @middleware ||= MiddlewareStack.new(self) @middleware.instance_eval(&) if block_given? @middleware end |
#mount(*controllers) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/lennarb/app.rb', line 63 def mount(*controllers) controllers.each do |controller| raise ArgumentError, "Controller must respond to :routes" unless controller.respond_to?(:routes) self.controllers << controller end end |