class Strelka::Session::Db

Database session class – this class offers persistent session data using any database that Sequel supports. It defaults to non-persistent, in memory Sqlite.

Constants

CONFIG_DEFAULTS

Configuration defaults

Public Class Methods

configure( options=nil ) click to toggle source

Configure the session class with the given options, which should be a Hash or an object that has a Hash-like interface.

Valid options (in addition to those ):

[cookie_name]

The name of the cookie to use for the session ID

[cookie_options]

Options to pass to Strelka::Cookie's constructor.

[connect]

The Sequel connection string; if nil, an in-memory DB will be used.

[table_name]

The name of the sessions table. Defaults to 'sessions'.

Calls superclass method Strelka::Session::Default::configure
# File lib/strelka/session/db.rb, line 137
def self::configure( options=nil )
        super

        if options && options[:connect]
                self.log.warn "Configuring dbsession with: %p" % [ options ]
                self.table_name = options[:table_name] || CONFIG_DEFAULTS[:table_name]
                self.db = Sequel.connect( options[:connect] )

                self.db.logger = Loggability[ Mongrel2 ].proxy_for( self.db )
                self.db.sql_log_level = :debug
                self.initialize_sessions_table
        end
end
delete_session_data( session_id ) click to toggle source

Delete the data associated with the given session_id from the DB.

# File lib/strelka/session/db.rb, line 111
def self::delete_session_data( session_id )
        self.dataset.filter( :session_id => session_id ).delete
end
has_session_for?( request ) click to toggle source

Return true if the given request has a session token which corresponds to an existing session key.

# File lib/strelka/session/db.rb, line 118
def self::has_session_for?( request )
        id = self.get_existing_session_id( request ) or return false
        return !self.dataset.filter( :session_id => id ).empty?
end
initialize_sessions_table() click to toggle source

Create the initial DB sessions schema if needed, setting the sessions attribute to a Sequel dataset on the configured DB table.

# File lib/strelka/session/db.rb, line 68
def self::initialize_sessions_table
        if self.db.table_exists?( self.table_name.to_sym )
                self.log.debug "Using existing sessions table for %p" % [ db ]

        else
                self.log.debug "Creating new sessions table for %p" % [ db ]
                self.db.create_table( self.table_name.to_sym ) do
                        text :session_id, :primary_key => true
                        text :session
                        timestamp :created
                end
        end

        self.dataset = self.db[ self.table_name.to_sym ]
end
load( session_id ) click to toggle source

Load a session instance from storage using the given session_id.

# File lib/strelka/session/db.rb, line 86
def self::load( session_id )
        session_row = self.dataset.filter( :session_id => session_id ).first
        session = if session_row.nil?
                        {}
                else
                        YAML.load( session_row[:session], safe: false )
                end
        return new( session_id, session )
end
save_session_data( session_id, data={} ) click to toggle source

Save the given data associated with the session_id to the DB.

# File lib/strelka/session/db.rb, line 98
def self::save_session_data( session_id, data={} )
        self.db.transaction do
                self.delete_session_data( session_id.to_s )
                self.dataset.insert(
                        :session_id => session_id,
                        :session    => data.to_yaml,
                        :created    => Time.now.utc.to_s
                )
        end
end

Public Instance Methods

dataset() click to toggle source

The Sequel dataset for the sessions table

# File lib/strelka/session/db.rb, line 50
singleton_attr_accessor :dataset
db() click to toggle source

The Sequel dataset connection

# File lib/strelka/session/db.rb, line 46
singleton_attr_accessor :db
dbsession() click to toggle source

Configurability API – use the 'dbsession' section of the config

# File lib/strelka/session/db.rb, line 25
config_key :dbsession
table_name() click to toggle source

The name of the table to use for storing sessions

# File lib/strelka/session/db.rb, line 54
singleton_attr_accessor :table_name