Class: Waves::Session
Overview
Encapsulates the session associated with a given request. A session has an expiration and path, which must be provided in a Waves::Configuration. Sensible defaults are defined in Waves::Configurations::Default
Class Method Summary collapse
- .base_path ⇒ Object
-
.generate_session_id ⇒ Object
Concoct a (probably) unique session id.
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.
-
#to_hash ⇒ Object
Return the session data as a hash.
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.
19 20 21 22 |
# File 'lib/runtime/session.rb', line 19 def initialize( request ) @request = request @data ||= ( File.exist?( session_path ) ? load_session : {} ) end |
Class Method Details
.base_path ⇒ Object
29 30 31 |
# File 'lib/runtime/session.rb', line 29 def self.base_path Waves::Application.instance.config.session[:path] end |
.generate_session_id ⇒ Object
Concoct a (probably) unique session id
9 10 11 12 13 |
# File 'lib/runtime/session.rb', line 9 def self.generate_session_id # from Camping ... chars = [*'A'..'Z'] + [*'0'..'9'] + [*'a'..'z'] (0..48).inject(''){|s,x| s+=chars[ rand(chars.length) ] } end |
Instance Method Details
#[](key) ⇒ Object
Access a given data element of the session using the given key.
45 |
# File 'lib/runtime/session.rb', line 45 def [](key) ; @data[key] ; end |
#[]=(key, val) ⇒ Object
Set the given data element of the session using the given key and value.
47 |
# File 'lib/runtime/session.rb', line 47 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.
35 36 37 38 39 40 41 42 |
# File 'lib/runtime/session.rb', line 35 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 |
#to_hash ⇒ Object
Return the session data as a hash
25 26 27 |
# File 'lib/runtime/session.rb', line 25 def to_hash @data.to_hash end |