Class: Waves::Session
Overview
Encapsulates the session associated with a given request. A session has an expiration and path. You must set these in your configuration file. See Waves::Configuration for more information.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access a given data element of the session using the given key.
-
#[]=(key, val) ⇒ Object
Set the given data element of the session using the given key and value.
-
#initialize(request) ⇒ Session
constructor
Create a new session object using the given request.
-
#save ⇒ Object
Save the session data.
Constructor Details
#initialize(request) ⇒ Session
Create a new session object using the given request. This is not necessarily the same as constructing a new session. The session_id cookie for the request domain is used to store a session id. The actual session data will be stored in a directory specified by the application’s configuration file.
13 14 15 16 |
# File 'lib/runtime/session.rb', line 13 def initialize( request ) @request = request @data ||= ( File.exist?( session_path ) ? load_session : {} ) end |
Instance Method Details
#[](key) ⇒ Object
Access a given data element of the session using the given key.
30 |
# File 'lib/runtime/session.rb', line 30 def [](key) ; @data[key] ; end |
#[]=(key, val) ⇒ Object
Set the given data element of the session using the given key and value.
32 |
# File 'lib/runtime/session.rb', line 32 def []=(key,val) ; @data[key] = val ; end |
#save ⇒ Object
Save the session data. You shouldn’t typically have to call this directly, since it is called by Waves::Response#finish.
20 21 22 23 24 25 26 27 |
# File 'lib/runtime/session.rb', line 20 def save if @data && @data.length > 0 File.write( session_path, @data.to_yaml ) @request.response.( 'session_id', :value => session_id, :path => '/', :expires => Time.now + Waves::Server.config.session[:duration] ) end end |