Class: Orange::Stack

Inherits:
Object show all
Defined in:
lib/orange-core/stack.rb

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

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.

Parameters:

  • app_class (Orange::Application) (defaults to: nil)

    the class of the main application

  • core (Orange::Core) (defaults to: false)

    the orange core

  • prebuilt (Symbol) (defaults to: :none)

    the optional prebuilt stack, if one isn’t passed as block



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

Parameters:

  • mod (Orange::Mixin)

    a mixin to be included in the packet



165
166
167
# File 'lib/orange-core/stack.rb', line 165

def add_pulp(mod)
  orange.add_pulp(mod)
end

#appObject

Builds the middleware stack (or uses a cached one)

If auto_reload is enabled (#auto_reload!), builds every time

Returns:

  • (Object)

    a full stack of middleware and the exit application, conforming to Rack guidelines



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_buildObject



210
211
212
# File 'lib/orange-core/stack.rb', line 210

def do_build
  @app = @build.to_app
end

#inspectObject

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_appObject

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

TODO:

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_recaptureObject

Turn off recapture middleware, which is normally just on top of the exit point

See Also:

  • Middleware::Recapture


108
109
110
# File 'lib/orange-core/stack.rb', line 108

def no_recapture
  @recapture = false
end

#orangeOrange::Core

Returns the Orange::Core

Returns:



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

TODO:

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.extract_options!
  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.options[: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.extract_options!
  @main_app = app
  @build.run(app)
end

#show_exceptionsObject

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

Parameters:

  • middleware (Object)

    A class of middleware that meets rack middleware requirements



71
72
73
# File 'lib/orange-core/stack.rb', line 71

def use(middleware, *args, &block)
  @build.use(middleware, *args, &block)
end

#use_exceptionsObject

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