Class: Strelka::Session::Db

Inherits:
Default show all
Extended by:
Configurability, Forwardable, MethodUtilities
Defined in:
lib/strelka/session/db.rb

Overview

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

Constant Summary collapse

CONFIG_DEFAULTS =

Configuration defaults

{
	connect: nil,
	table_name: 'sessions',
	cookie_name: 'strelka-session',
	cookie_options: {
		expires: "+1d",
	}
}

Instance Attribute Summary

Attributes inherited from Default

#namespace

Attributes inherited from Strelka::Session

#session_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodUtilities

attr_predicate, attr_predicate_accessor, singleton_attr_accessor, singleton_attr_reader, singleton_attr_writer, singleton_method_alias, singleton_predicate_accessor, singleton_predicate_reader

Methods inherited from Default

#cookie_name, #defaultsession, #destroy, get_existing_session_id, get_session_id, #initialize, #initialize_clone, #initialize_dup, load_session_data, #namespaced_hash, #save, #sessions

Methods included from Delegation

def_class_delegators, def_ivar_delegators, def_method_delegators

Methods included from DataUtilities

autovivify, deep_copy, stringify_keys, symbolify_keys

Methods inherited from Strelka::Session

get_existing_session_id, get_session_id, #initialize, load_or_create, load_session_data

Methods included from AbstractClass

extended, included, #inherited, #pure_virtual

Constructor Details

This class inherits a constructor from Strelka::Session::Default

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Strelka::Session::Default

Class Method Details

.configure(options = nil) ⇒ Object

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



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/strelka/session/db.rb', line 138

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) ⇒ Object

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



112
113
114
# File 'lib/strelka/session/db.rb', line 112

def self::delete_session_data( session_id )
	self.dataset.filter( :session_id => session_id ).delete
end

.has_session_for?(request) ⇒ Boolean

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

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/strelka/session/db.rb', line 119

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_tableObject

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/strelka/session/db.rb', line 69

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) ⇒ Object

Load a session instance from storage using the given session_id.



87
88
89
90
91
92
93
94
95
# File 'lib/strelka/session/db.rb', line 87

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 = {}) ⇒ Object

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



99
100
101
102
103
104
105
106
107
108
# File 'lib/strelka/session/db.rb', line 99

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

Instance Method Details

The configured session cookie parameters



59
# File 'lib/strelka/session/db.rb', line 59

singleton_attr_accessor :cookie_options

#datasetObject

The Sequel dataset for the sessions table



51
# File 'lib/strelka/session/db.rb', line 51

singleton_attr_accessor :dataset

#dbObject

The Sequel dataset connection



47
# File 'lib/strelka/session/db.rb', line 47

singleton_attr_accessor :db

#dbsessionObject

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



26
# File 'lib/strelka/session/db.rb', line 26

config_key :dbsession

#table_nameObject

The name of the table to use for storing sessions



55
# File 'lib/strelka/session/db.rb', line 55

singleton_attr_accessor :table_name