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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/orange-core/stack.rb', line 29

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 
    @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



89
90
91
# File 'lib/orange-core/stack.rb', line 89

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
  @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)} @build=#{@build.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



77
78
79
# File 'lib/orange-core/stack.rb', line 77

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)



64
65
66
# File 'lib/orange-core/stack.rb', line 64

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


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

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.



50
51
52
53
54
55
56
57
# File 'lib/orange-core/stack.rb', line 50

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/orange-core/stack.rb', line 122

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
  use Rack::AbstractFormat unless opts[:no_abstract_format] 
      # Must be used before non-destructive route altering done by Orange,
      # since all orange stuff is non-destructive
  stack Orange::Middleware::RouteSite, opts.dup
  stack Orange::Middleware::RouteContext, opts.dup
  stack Orange::Middleware::Database
  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



100
101
102
# File 'lib/orange-core/stack.rb', line 100

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



83
84
85
# File 'lib/orange-core/stack.rb', line 83

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



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

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

#use_exceptionsObject

Shortcut for adding Orange::Middleware::ShowExceptions to the middleware stack



95
96
97
# File 'lib/orange-core/stack.rb', line 95

def use_exceptions
  stack Orange::Middleware::ShowExceptions
end