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
91 92 93 94 95 96 97 |
# File 'lib/lennarb/app.rb', line 91 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
100 101 102 |
# File 'lib/lennarb/app.rb', line 100 def env @env end |
#root ⇒ Object
The root directory
103 104 105 |
# File 'lib/lennarb/app.rb', line 103 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?(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)
169 170 171 |
# File 'lib/lennarb/app.rb', line 169 def after(&block) self.class.after(&block) end |
#app ⇒ #call
Get the Rack app with middleware
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/lennarb/app.rb', line 118 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)
161 162 163 |
# File 'lib/lennarb/app.rb', line 161 def before(&block) self.class.before(&block) end |
#call(env) ⇒ Array
Rack interface method
134 135 136 137 |
# File 'lib/lennarb/app.rb', line 134 def call(env) env[RACK_LENNA_APP] = self app.call(env) end |
#config(*envs) { ... } ⇒ Config
Get/define configuration
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/lennarb/app.rb', line 190 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)
153 154 155 |
# File 'lib/lennarb/app.rb', line 153 def helpers(&block) self.class.helpers(&block) end |
#initialize! ⇒ self
Initialize the app
205 206 207 208 209 210 211 |
# File 'lib/lennarb/app.rb', line 205 def initialize! raise AlreadyInitializedError if @initialized @initialized = true routes.freeze self end |
#initialized? ⇒ Boolean
Check if initialized
216 217 218 |
# File 'lib/lennarb/app.rb', line 216 def initialized? @initialized end |
#middleware { ... } ⇒ MiddlewareStack
Define middleware
143 144 145 146 147 |
# File 'lib/lennarb/app.rb', line 143 def middleware(&block) @middleware_stack ||= default_middleware_stack @middleware_stack.instance_eval(&block) if block_given? @middleware_stack end |
#routes { ... } ⇒ Routes
Get/define routes
177 178 179 180 181 182 183 |
# File 'lib/lennarb/app.rb', line 177 def routes(&block) if block_given? self.class.instance_exec(&block) end self.class.routes end |