Class: Lennarb::App
- Inherits:
-
Object
- Object
- Lennarb::App
- Defined in:
- lib/lennarb/app.rb
Overview
Main application class with hooks and helpers support
Constant Summary collapse
- AlreadyInitializedError =
Error for already initialized app
Class.new(StandardError)
Class Attribute Summary collapse
-
.app ⇒ String
Rack environment variable name.
Instance Attribute Summary collapse
-
#env ⇒ Object
The current environment.
-
#root ⇒ Object
The root directory.
Class Method Summary collapse
-
.after {|req, res| ... } ⇒ Array
Define an after hook.
-
.before {|req, res| ... } ⇒ Array
Define a before hook.
-
.config(*envs) { ... } ⇒ Config
Define configuration.
-
.helpers(mod_or_block = nil) { ... } ⇒ Module
Define helper methods for the application.
-
.inherited(subclass) ⇒ void
Set up subclass.
-
.root {|req, res, params| ... } ⇒ void
Define root route (GET /).
-
.routes ⇒ Routes
Get routes for this app class.
Instance Method Summary collapse
-
#after {|req, res| ... } ⇒ Array
Define after hook (instance method).
-
#app ⇒ #call
Get the Rack app with middleware.
-
#before {|req, res| ... } ⇒ Array
Define before hook (instance method).
-
#call(env) ⇒ Array
Rack interface method.
-
#config(*envs) { ... } ⇒ Config
Get/define configuration.
-
#helpers { ... } ⇒ Module
Define helpers (instance method).
-
#initialize {|self| ... } ⇒ App
constructor
Initialize a new app.
-
#initialize! ⇒ self
Initialize the app.
-
#initialized? ⇒ Boolean
Check if initialized.
-
#middleware { ... } ⇒ MiddlewareStack
Define middleware.
-
#routes { ... } ⇒ Routes
Get/define routes.
Constructor Details
#initialize {|self| ... } ⇒ App
Initialize a new app
103 104 105 106 107 108 109 |
# File 'lib/lennarb/app.rb', line 103 def initialize(&block) @initialized = false @root = Pathname.pwd @env = Environment.new(compute_env) instance_eval(&block) if block_given? end |
Class Attribute Details
.app ⇒ String
Rack environment variable name
9 10 11 |
# File 'lib/lennarb/app.rb', line 9 def app @app ||= self end |
Instance Attribute Details
#env ⇒ Object
The current environment
112 113 114 |
# File 'lib/lennarb/app.rb', line 112 def env @env end |
#root ⇒ Object
The root directory
115 116 117 |
# File 'lib/lennarb/app.rb', line 115 def root @root end |
Class Method Details
.after {|req, res| ... } ⇒ Array
Define an after hook
33 34 35 |
# File 'lib/lennarb/app.rb', line 33 def after(&block) Hooks.add(self, :after, &block) end |
.before {|req, res| ... } ⇒ Array
Define a before hook
25 26 27 |
# File 'lib/lennarb/app.rb', line 25 def before(&block) Hooks.add(self, :before, &block) end |
.config(*envs) { ... } ⇒ Config
Define configuration
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lennarb/app.rb', line 74 def config(*envs, &) @config ||= Config.new(self) if block_given? write = envs.empty? || envs.map(&:to_sym).include?(compute_env_name) @config.instance_eval(&) if write end @config end |
.helpers(mod_or_block = nil) { ... } ⇒ Module
Define helper methods for the application
17 18 19 |
# File 'lib/lennarb/app.rb', line 17 def helpers(mod_or_block = nil, &block) Helpers.define(self, mod_or_block, &block) end |
.inherited(subclass) ⇒ void
This method returns an undefined value.
Set up subclass
48 49 50 51 52 |
# File 'lib/lennarb/app.rb', line 48 def inherited(subclass) super # Each subclass gets its own routes subclass.instance_variable_set(:@routes, Routes.new) end |
.root {|req, res, params| ... } ⇒ void
This method returns an undefined value.
Define root route (GET /)
65 66 67 |
# File 'lib/lennarb/app.rb', line 65 def root(&block) get("/", &block) end |
Instance Method Details
#after {|req, res| ... } ⇒ Array
Define after hook (instance method)
181 182 183 |
# File 'lib/lennarb/app.rb', line 181 def after(&block) self.class.after(&block) end |
#app ⇒ #call
Get the Rack app with middleware
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/lennarb/app.rb', line 130 def app @app ||= begin handler = build_request_handler stack = middleware.to_a Rack::Builder.app do stack.each { |middleware, args, block| use(middleware, *args, &block) } run handler end end end |
#before {|req, res| ... } ⇒ Array
Define before hook (instance method)
173 174 175 |
# File 'lib/lennarb/app.rb', line 173 def before(&block) self.class.before(&block) end |
#call(env) ⇒ Array
Rack interface method
146 147 148 149 |
# File 'lib/lennarb/app.rb', line 146 def call(env) env[RACK_LENNA_APP] = self app.call(env) end |
#config(*envs) { ... } ⇒ Config
Get/define configuration
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/lennarb/app.rb', line 204 def config(*envs, &) @config ||= self.class.config if block_given? write = envs.empty? || envs.map(&:to_sym).include?(env.name) @config.instance_eval(&) if write end @config end |
#helpers { ... } ⇒ Module
Define helpers (instance method)
165 166 167 |
# File 'lib/lennarb/app.rb', line 165 def helpers(&block) self.class.helpers(&block) end |
#initialize! ⇒ self
Initialize the app
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/lennarb/app.rb', line 219 def initialize! raise AlreadyInitializedError if @initialized warn_about_defaulted_env # Snapshot the class routes into this instance and freeze only the copy, # so booting an app never freezes process-global class state. @routes = Routes.new @routes.merge!(self.class.routes) @routes.freeze @initialized = true self end |
#initialized? ⇒ Boolean
Check if initialized
237 238 239 |
# File 'lib/lennarb/app.rb', line 237 def initialized? @initialized end |
#middleware { ... } ⇒ MiddlewareStack
Define middleware
155 156 157 158 159 |
# File 'lib/lennarb/app.rb', line 155 def middleware(&block) @middleware_stack ||= default_middleware_stack @middleware_stack.instance_eval(&block) if block_given? @middleware_stack end |
#routes { ... } ⇒ Routes
Get/define routes
189 190 191 192 193 194 195 196 197 |
# File 'lib/lennarb/app.rb', line 189 def routes(&block) if block_given? self.class.instance_exec(&block) end # Before boot, route definitions go to the class. After boot, this # instance serves from its own frozen snapshot. @routes || self.class.routes end |