Db

class
Superclass
Strelka::Session::Default
Extended With
Configurability
Forwardable
Strelka::MethodUtilities

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

anchor
configure( options=nil )

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'.

# File lib/strelka/session/db.rb, line 136
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
anchor
delete_session_data( session_id )

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

# File lib/strelka/session/db.rb, line 110
def self::delete_session_data( session_id )
        self.dataset.filter( :session_id => session_id ).delete
end
anchor
has_session_for?( request )

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

# File lib/strelka/session/db.rb, line 117
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
anchor
initialize_sessions_table()

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 67
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
anchor
load( session_id )

Load a session instance from storage using the given session_id.

# File lib/strelka/session/db.rb, line 85
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
anchor
save_session_data( session_id, data={} )

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

# File lib/strelka/session/db.rb, line 97
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

anchor anchor
dataset()

The Sequel dataset for the sessions table

# File lib/strelka/session/db.rb, line 49
singleton_attr_accessor :dataset
anchor
db()

The Sequel dataset connection

# File lib/strelka/session/db.rb, line 45
singleton_attr_accessor :db
anchor
dbsession()

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

# File lib/strelka/session/db.rb, line 24
config_key :dbsession
anchor
table_name()

The name of the table to use for storing sessions

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