Sha256: d777d4bffc6360fefafab98dcc6d61a0ecd2f8fd0b98585b55915ce23fd029d4

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'nano/kernel/assign_with'

require 'nitro/cgi'
require 'nitro/cgi/request'
require 'nitro/cgi/response'
require 'nitro/render'
require 'nitro/session'

module Nitro
  
# Encapsulates an HTTP processing cycle context.
# Integrates the HTTP Request, the HTTP Response
# and the Render used to generate the response
# body.
# The Context object can be accessed by the
# context, request or response aliases. You can
# use the alias that makes sense every time.

class Context
  include Request
  include Response
  include Render

  # The configuration parameters.

  attr_accessor :conf

  # The session contains variables that stay alive 
  # for the full user session. Session variables
  # should be generally avoided. This variable
  # becomes populated ONLY if needed.

  attr_reader :session

  # The dispatcher.

  attr_accessor :dispatcher

  def initialize(conf)
    @conf = conf
    @dispatcher = @conf.dispatcher
    @context = self

    # initialize response.
    
    @status = Http::STATUS_OK
    @response_headers = { 'Content-Type' => 'text/html' }

    # initialize the output buffer.

    @out ||= OutputBuffer.new
  end

  # Close the context, should be called at the 
  # end of the HTTP request handling code.

  def close
    @session.sync if @session
  end
  alias_method :finish, :close

  #--
  # FIXME: something more elegant/efficient.
  #++

  def out
    if @out == '(error)'
      @out = ''
      render '/error'
      @out
    else
      @out
    end
  end

  # Lazy lookup of the session to avoid costly cookie
  # lookup when not needed.

  def session
    @session || @session = Session.lookup(self)
  end

  # Populate an object from request parameters.
  # This is a truly dangerous method.
  #
  # === Options
  #
  # * name
  # * force_boolean
  
  def fill(obj, options = {})
    Property.populate_object(obj, @params, options)
  end
  alias_method :populate, :fill
  alias_method :assign, :fill  
  
end

end

# * George Moschovitis <gm@navel.gr>

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.24.0 lib/nitro/context.rb