Strelka::HTTPResponse::

Session

module
Included Modules
Strelka::Constants

The mixin that adds methods to Strelka::HTTPResponse for session persistance. If you create a response via the Request#response method, the session will added to the response as well, and automatically saved after the handler and all plugins have run. Or you can do so manually:

response = request.response
response.save_session

You can also clear the session with destroy_session.

Attributes

session_namespace[R]

The current session namespace

Public Class Methods

anchor
new( * )

Initialize instance variables for session data in the response.

# File lib/strelka/httpresponse/session.rb, line 26
def initialize( * )
        super
        @session = nil
        @session_namespace = nil
end

Public Instance Methods

anchor
destroy_session()

Purge the response's session from the session store and expire its ID.

# File lib/strelka/httpresponse/session.rb, line 94
def destroy_session
        if self.session?
                self.log.debug "Destroying session: %p" % [ self.session ]
                self.session.destroy( self )
                self.request.session = @session = nil
        else
                self.log.debug "No session to destroy."
        end
end
anchor
has_session?()
Alias for: session?
anchor
save_session()

Tell the associated session to save itself and set up the session ID in the response, if one exists.

# File lib/strelka/httpresponse/session.rb, line 107
def save_session
        if self.session_loaded?
                self.log.debug "Saving session: %p" % [ self.session ]
                self.session.save( self )
        else
                self.log.debug "No session to save."
        end
end
anchor
session()

Return the session associated with the response, creating it if necessary.

# File lib/strelka/httpresponse/session.rb, line 52
def session
        unless @session
                # Load the session from the associated request if there is one.
                # If there isn't an associated request, this will just create a
                # new blank session.
                if self.request.session?
                        self.log.debug "Getting the request's session."
                        self.session = request.session
                else
                        self.log.debug "No session loaded in the request; creating it in the response."
                        self.session = Strelka::App::Sessions.session_class.new
                end
        end

        return @session
end
anchor
session=( new_session )

Set the request's session object.

# File lib/strelka/httpresponse/session.rb, line 71
def session=( new_session )
        self.log.debug "Setting session to %p in namespace %p" % [ new_session, self.session_namespace ]
        new_session.namespace = self.session_namespace
        @session = new_session
        self.log.debug "  session is: %p" % [ @session ]
        # request.session = new_session # should it set the session in the request too?
end
anchor
session?()

Returns true if the response already has an associated session object.

# File lib/strelka/httpresponse/session.rb, line 81
def session?
        return @session || self.request.session?
end
Also aliased as: has_session?
anchor
session_loaded?()

Returns true if the response or its request has already loaded the session.

# File lib/strelka/httpresponse/session.rb, line 88
def session_loaded?
        return @session || self.request.session_loaded?
end
anchor
session_namespace=( namespace )

The namespace that will be used when creating a session for this response

# File lib/strelka/httpresponse/session.rb, line 42
def session_namespace=( namespace )
        self.log.debug "Setting session namespace to %p" % [ namespace ]
        @session_namespace = namespace

        # If the session has already been created, switch its current namespace
        @session.namespace = namespace if @session
end