Class: Rack::Session::Mongo

Inherits:
Abstract::ID
  • Object
show all
Defined in:
lib/mongo-rack-session.rb

Defined Under Namespace

Classes: Session

Constant Summary collapse

VERSION =
'0.1.1'
DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge \
:mongo_db_name => 'shiro',
:mongo_collection_name => 'Sessions',
:mongo_server => '10.0.0.82',
:default_key => 'JSESSIONID',  # cookie key
:session_class_key => 'Id'

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Mongo

Returns a new instance of Mongo.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongo-rack-session.rb', line 34

def initialize(app, options={})
  super
  @key = options[:key] || @default_options[:default_key]

  # set up the Session Class either defined by the User or using the one defined above
  @@session_class = options[:session_class] || Session
  @@session_class.set_database_name(options[:mongo_db_name] || @default_options[:mongo_db_name])
  @@session_class.set_collection_name(options[:mongo_collection_name] || @default_options[:mongo_collection_name])
  @@session_class.connection(::Mongo::Connection.new(options[:mongo_server] || @default_options[:mongo_server]))
  @@session_class_key = options[:session_class_key] || @default_options[:session_class_key]
end

Instance Method Details

#find_session(sid) ⇒ Object



56
57
58
59
# File 'lib/mongo-rack-session.rb', line 56

def find_session(sid)
  mongo_session = @@session_class.first(:conditions => {@@session_class_key => sid})
  (mongo_session && mongo_session.is_valid?) || @@session_class.create(@@session_class_key => sid)
end

#generate_sidObject



61
62
63
# File 'lib/mongo-rack-session.rb', line 61

def generate_sid
  UUID.generate
end

#get_session(env, sid) ⇒ Object



46
47
48
49
50
# File 'lib/mongo-rack-session.rb', line 46

def get_session(env, sid)
  sid ||= generate_sid
  session = find_session(sid)
  [sid, session]
end

#set_session(env, session_id, new_session, options) ⇒ Object



52
53
54
# File 'lib/mongo-rack-session.rb', line 52

def set_session(env, session_id, new_session, options)
  #new_session.save ? new_session.Id : nil
end