Class: Orange::Stack
Overview
Builds an orange stack of middleware Use in the rackup file as follows: app = Orange::Stack.new do
stack Orange::DataMapper 'sqlite3::memory:' <= loads orange specific middleware
use OtherMiddleware
run SomeApp.new
end run app
All middleware placed inside the Orange::Stack will have access to the Orange Core (as long as it’s been written to accept it as the second initialization argument) when added with the ‘stack’ method
In general, Orange::Stack works like Rack::Builder.
Instance Method Summary collapse
-
#add_pulp(mod) ⇒ Object
Adds pulp to the core via the Orange::Core#add_pulp method.
-
#app ⇒ Object
Builds the middleware stack (or uses a cached one).
-
#auto_reload!(val = true) ⇒ Object
Set the auto_reload option, called without args, defaults to true, other option is to set it to false.
-
#call(env) ⇒ Object
Sets the core and then passes on to the stack, according to standard rack procedure.
- #do_build ⇒ Object
-
#initialize(app_class = nil, core = false, prebuilt = :none, &block) ⇒ Stack
constructor
Creates a new Orange::Stack out of the passed block.
-
#inspect ⇒ Object
Debug helping.
-
#load(*args, &block) ⇒ Object
Loads resources into the core using the Orange::Core#load method.
-
#main_app ⇒ Object
Returns the main application instance that was added by the run method.
-
#map(path, &block) ⇒ Object
Passes through to Rack::Builder#map.
-
#no_recapture ⇒ Object
Turn off recapture middleware, which is normally just on top of the exit point.
-
#orange ⇒ Orange::Core
Returns the Orange::Core.
- #postrouting(opts = {}) ⇒ Object
-
#prebuild(choice) ⇒ Object
Runs methods necessary to build a stack.
-
#prerouting(*args) ⇒ Object
A shortcut for adding many of the routing middleware options simultaneously.
- #responders(opts = {}) ⇒ Object
-
#routing(opts = {}) ⇒ Object
A shortcut for routing via Orange::Middleware::RestfulRouter and any plugins.
-
#run(app, *args) ⇒ Object
The exit point for the middleware stack, add the app to @main_app and then call Rack::Builder#run with the main app.
-
#show_exceptions ⇒ Object
Alias for use_exceptions.
-
#stack(middleware, *args, &block) ⇒ Object
Adds Orange-aware middleware using the Rack::Builder#use method, adding the orange core to the args passed on.
-
#use(middleware, *args, &block) ⇒ Object
Adds middleware using the Rack::Builder#use method.
-
#use_exceptions ⇒ Object
Shortcut for adding Orange::Middleware::ShowExceptions to the middleware stack.
Constructor Details
#initialize(app_class = nil, core = false, prebuilt = :none, &block) ⇒ Stack
Creates a new Orange::Stack out of the passed block.
If a block is not passed, it will try to build one from scratch. The bare minimum will be ‘run app_class.new(@core)`, there are also other stacks that can be used.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/orange-core/stack.rb', line 30 def initialize(app_class = nil, core = false, prebuilt = :none, &block) @build = Rack::Builder.new @core = core || Orange::Core.new @core.stack = self # Set a back reference in the core. @auto_reload = false @app = false @middleware = [] @recapture = true @main_app = app_class if block_given? instance_eval(&block) else @main_app = app_class.new(@core) unless app_class.nil? prebuild(prebuilt) end end |
Instance Method Details
#add_pulp(mod) ⇒ Object
Adds pulp to the core via the Orange::Core#add_pulp method
165 166 167 |
# File 'lib/orange-core/stack.rb', line 165 def add_pulp(mod) orange.add_pulp(mod) end |
#app ⇒ Object
Builds the middleware stack (or uses a cached one)
If auto_reload is enabled (#auto_reload!), builds every time
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/orange-core/stack.rb', line 198 def app if @auto_reload orange.fire(:stack_reloading, @app) if orange.stack # Alert we are rebuilding @app = false # Rebuild no matter what if autoload end unless @app do_build # Build if necessary orange.fire(:stack_loaded, @app) end @app end |
#auto_reload!(val = true) ⇒ Object
Set the auto_reload option, called without args, defaults to true, other option is to set it to false
90 91 92 |
# File 'lib/orange-core/stack.rb', line 90 def auto_reload!(val = true) @auto_reload = val end |
#call(env) ⇒ Object
Sets the core and then passes on to the stack, according to standard rack procedure
216 217 218 219 |
# File 'lib/orange-core/stack.rb', line 216 def call(env) env['orange.core'] = @core app.call(env) end |
#do_build ⇒ Object
210 211 212 |
# File 'lib/orange-core/stack.rb', line 210 def do_build @app = @build.to_app end |
#inspect ⇒ Object
Debug helping
222 223 224 |
# File 'lib/orange-core/stack.rb', line 222 def inspect "#<Orange::Stack:0x#{self.object_id.to_s(16)} @app=#{@app.inspect}, @core=#{@core.inspect}>" end |
#load(*args, &block) ⇒ Object
Loads resources into the core using the Orange::Core#load method
all args are passed on
78 79 80 |
# File 'lib/orange-core/stack.rb', line 78 def load(*args, &block) orange.load(*args, &block) end |
#main_app ⇒ Object
Returns the main application instance that was added by the run method. Obviously won’t return anything useful if the middleware stack hasn’t been set up with an explicit exit point, as could be the case for a pure orange middleware stack on top of a different exit application (like Sinatra or Rails)
65 66 67 |
# File 'lib/orange-core/stack.rb', line 65 def main_app @main_app end |
#map(path, &block) ⇒ Object
Make this work - passing the block on to builder means we can’t intercept anything, which will yield unexpected results
Passes through to Rack::Builder#map
187 188 189 190 |
# File 'lib/orange-core/stack.rb', line 187 def map(path, &block) raise 'not yet supported' @build.map(path, &block) end |
#no_recapture ⇒ Object
Turn off recapture middleware, which is normally just on top of the exit point
108 109 110 |
# File 'lib/orange-core/stack.rb', line 108 def no_recapture @recapture = false end |
#orange ⇒ Orange::Core
Returns the Orange::Core
179 180 181 |
# File 'lib/orange-core/stack.rb', line 179 def orange @core end |
#postrouting(opts = {}) ⇒ Object
145 146 147 148 149 |
# File 'lib/orange-core/stack.rb', line 145 def postrouting(opts ={}) orange.plugins.each{|p| p.middleware(:postrouting).each{|m| stack m, opts.dup} if p.has_middleware?} stack Orange::Middleware::Template stack Orange::Middleware::FourOhFour, opts.dup # Last ditch, send route to 404 page. end |
#prebuild(choice) ⇒ Object
Offer more choices for default stacks
Runs methods necessary to build a stack. Don’t use if a stack has already been built by the initialize block.
51 52 53 54 55 56 57 58 |
# File 'lib/orange-core/stack.rb', line 51 def prebuild(choice) case choice when :none run @main_app else run @main_app end end |
#prerouting(*args) ⇒ Object
A shortcut for adding many of the routing middleware options simultaneously. Includes:
-
Orange::Middleware::Rerouter
-
Orange::Middleware::Static
-
Rack::AbstractFormat
-
Orange::Middleware::RouteSite
-
Orange::Middleware::RouteContext
All of these are passed the args hash to use as they will, except for Rack::AbstractFormat
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/orange-core/stack.rb', line 123 def prerouting(*args) opts = args. stack Orange::Middleware::Globals stack Orange::Middleware::Loader stack Orange::Middleware::Rerouter, opts.dup stack Orange::Middleware::Static, opts.dup stack Orange::Middleware::AbstractFormat unless opts[:no_abstract_format] stack Orange::Middleware::RootDir, opts.dup stack Orange::Middleware::RouteSite, opts.dup stack Orange::Middleware::RouteContext, opts.dup stack Orange::Middleware::Database unless (opts[:no_datamapper] || orange.[:no_datamapper]) orange.plugins.each{|p| p.middleware(:prerouting).each{|m| stack m, opts.dup} if p.has_middleware?} end |
#responders(opts = {}) ⇒ Object
151 152 153 |
# File 'lib/orange-core/stack.rb', line 151 def responders(opts ={}) orange.plugins.each{|p| p.middleware(:responders).each{|m| stack m, opts.dup} if p.has_middleware?} end |
#routing(opts = {}) ⇒ Object
A shortcut for routing via Orange::Middleware::RestfulRouter and any plugins
Any args are passed on to the middleware
140 141 142 143 |
# File 'lib/orange-core/stack.rb', line 140 def routing(opts ={}) stack Orange::Middleware::RestfulRouter, opts.dup orange.plugins.each{|p| p.middleware(:routing).each{|m| stack m, opts.dup} if p.has_middleware?} end |
#run(app, *args) ⇒ Object
The exit point for the middleware stack, add the app to @main_app and then call Rack::Builder#run with the main app
171 172 173 174 175 |
# File 'lib/orange-core/stack.rb', line 171 def run(app, *args) opts = args. @main_app = app @build.run(app) end |
#show_exceptions ⇒ Object
Alias for use_exceptions
101 102 103 |
# File 'lib/orange-core/stack.rb', line 101 def show_exceptions use_exceptions end |
#stack(middleware, *args, &block) ⇒ Object
Adds Orange-aware middleware using the Rack::Builder#use method, adding the orange core to the args passed on
84 85 86 |
# File 'lib/orange-core/stack.rb', line 84 def stack(middleware, *args, &block) @build.use(middleware, @core, *args, &block) end |
#use(middleware, *args, &block) ⇒ Object
Adds middleware using the Rack::Builder#use method
71 72 73 |
# File 'lib/orange-core/stack.rb', line 71 def use(middleware, *args, &block) @build.use(middleware, *args, &block) end |
#use_exceptions ⇒ Object
Shortcut for adding Orange::Middleware::ShowExceptions to the middleware stack
96 97 98 |
# File 'lib/orange-core/stack.rb', line 96 def use_exceptions stack Orange::Middleware::ShowExceptions unless ENV['RACK_ENV'] == 'production' end |