Class: Strelka::Session

Inherits:
Object
  • Object
show all
Extended by:
Loggability, Pluggability, AbstractClass
Defined in:
lib/strelka/session.rb

Overview

Abstract base class for pluggable session strategies for the Sessions plugin.

To define your own session type, you'll need to inherit this class (either directly or via a subclass), name it Strelka::Session::{Something}, save it in a file named strelka/session/{something}.rb, and override the required methods.

The class methods you'll need to provide implementations for are:

  • self.configure
  • self.get_session_id
  • self.get_existing_session_id
  • self.load_session_data
  • self.save_session_data
  • self.delete_session_data

The instance methods fetch and set values in the session itself, and manipulate the namespace that's used to partition session data between applications:

  • #[]
  • #[]=
  • #save
  • #delete
  • #key?
  • #namespace=
  • #namespace

These methods provide basic functionality, but you might find it more efficient to override them:

  • self.load_or_create
  • self.load

Direct Known Subclasses

Default

Defined Under Namespace

Classes: Db, Default

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AbstractClass

extended, included, inherited, pure_virtual

Constructor Details

#initialize(session_id = nil, initial_values = {}) ⇒ Session

Set up a new instance with the given session_id and initial_values.



135
136
137
# File 'lib/strelka/session.rb', line 135

def initialize( session_id=nil, initial_values={} ) # :notnew:
	@session_id = session_id || self.class.get_session_id
end

Instance Attribute Details

#session_idObject (readonly)

The key of the session in the session store



145
146
147
# File 'lib/strelka/session.rb', line 145

def session_id
  @session_id
end

Class Method Details

.configure(options) ⇒ Object

Configure the session class with the given options, which should be a Hash or an object that has a Hash-like interface. This is a no-op by default.



63
64
# File 'lib/strelka/session.rb', line 63

def self::configure( options )
end

.delete_session_data(session_id) ⇒ Object

Delete the session data for the specified session_id.



97
98
# File 'lib/strelka/session.rb', line 97

def self::delete_session_data( session_id )
end

.get_existing_session_id(request) ⇒ Object

Fetch the session ID from the given +request, returning nil if it doesn't have any session attributes. You should override this and provide the code which fetches the ID from the request.



70
71
72
# File 'lib/strelka/session.rb', line 70

def self::get_existing_session_id( request )
	return nil
end

.get_session_id(request = nil) ⇒ Object

Fetch the session ID from the given request, or create a new one if the request is nil or doesn't have the necessary attributes. You should override this, as the default implementation just returns nil.



78
79
80
# File 'lib/strelka/session.rb', line 78

def self::get_session_id( request=nil )
	return nil
end

.has_session_for?(request) ⇒ Boolean

Return true if the given request has a valid session token, and that token corresponds to an existing session ID. You should override this if there's a cheaper way to check for an existing session than just calling ::load_session_data.

Returns:

  • (Boolean)


124
125
126
127
# File 'lib/strelka/session.rb', line 124

def self::has_session_for?( request )
	id = self.get_existing_session_id( request ) or return false
	return true if self.load( id )
end

.load(session_id) ⇒ Object

Load a session instance from storage using the given session_id and return it. Returns nil if no session could be loaded. You can either overload ::load_session_data if you are loading a data structure, or override ::load if you're serializing the session object directly.



105
106
107
108
# File 'lib/strelka/session.rb', line 105

def self::load( session_id )
	values = self.load_session_data( session_id ) or return nil
	return new( session_id, values )
end

.load_or_create(request = nil) ⇒ Object

Return an instance of the session class that's associated with the specified request, either loading one from the persistant store, or creating a new one if it can't be loaded or request is nil.



114
115
116
117
# File 'lib/strelka/session.rb', line 114

def self::load_or_create( request=nil )
	session_id = self.get_session_id( request )
	return self.load( session_id ) || new( session_id )
end

.load_session_data(session_id) ⇒ Object

Load session data for the specified session_id. This should return a data structure that can #merge!, or nil if there was no existing session data associated with session_id.



86
87
88
# File 'lib/strelka/session.rb', line 86

def self::load_session_data( session_id )
	return nil
end

.save_session_data(session_id, data) ⇒ Object

Save the session data for the specified session_id.



92
93
# File 'lib/strelka/session.rb', line 92

def self::save_session_data( session_id, data )
end