File: current.rb

package info (click to toggle)
libinnate-ruby 2010.07-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 812 kB
  • ctags: 621
  • sloc: ruby: 4,242; makefile: 2
file content (35 lines) | stat: -rw-r--r-- 972 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'innate/request'
require 'innate/response'

module Innate
  # We track the current request/response/session (Trinity) in Thread.current
  # so we can reach them from anywhere in the code without passing around the
  # objects directly.
  class Current
    extend Trinity

    def initialize(app, *rest)
      if rest.empty?
        @app = app
      else
        @app = Rack::Cascade.new([app, *rest])
      end
    end

    # Run setup and call the app
    def call(env)
      setup(env)
      @app.call(env)
    end

    # Setup new Request/Response/Session for this request/response cycle.
    # The parameters are here to allow Ramaze to inject its own classes.
    def setup(env, request = Request, response = Response, session = Session)
      current = Thread.current
      req = current[:request] = request.new(env)
      res = current[:response] = response.new
      current[:actions] = []
      current[:session] = Session.new(req, res)
    end
  end
end