Module: Strelka::HTTPResponse::Session

Includes:
Constants
Defined in:
lib/strelka/httpresponse/session.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#session_namespaceObject

The current session namespace



39
40
41
# File 'lib/strelka/httpresponse/session.rb', line 39

def session_namespace
  @session_namespace
end

Instance Method Details

#destroy_sessionObject

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



95
96
97
98
99
100
101
102
103
# File 'lib/strelka/httpresponse/session.rb', line 95

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

#initializeObject

Initialize instance variables for session data in the response.



27
28
29
30
31
# File 'lib/strelka/httpresponse/session.rb', line 27

def initialize( * )
  super
  @session = nil
  @session_namespace = nil
end

#save_sessionObject

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



108
109
110
111
112
113
114
115
# File 'lib/strelka/httpresponse/session.rb', line 108

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

#sessionObject

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/strelka/httpresponse/session.rb', line 53

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

#session=(new_session) ⇒ Object

Set the request's session object.



72
73
74
75
76
77
78
# File 'lib/strelka/httpresponse/session.rb', line 72

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

#session?Boolean Also known as: has_session?

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

Returns:

  • (Boolean)


82
83
84
# File 'lib/strelka/httpresponse/session.rb', line 82

def session?
  return @session || self.request.session?
end

#session_loaded?Boolean

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

Returns:

  • (Boolean)


89
90
91
# File 'lib/strelka/httpresponse/session.rb', line 89

def session_loaded?
  return @session || self.request.session_loaded?
end