Class: Rack::Session::Moped
- Inherits:
-
Abstract::ID
- Object
- Abstract::ID
- Rack::Session::Moped
- Defined in:
- lib/rack/session/moped.rb,
lib/rack/session/moped/version.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge( db: :rack, collection: :sessions, drop: false, seeds: ["localhost:27017"] )
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#marshal_data ⇒ Object
readonly
Returns the value of attribute marshal_data.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Instance Method Summary collapse
- #destroy_session(env, sid, options) ⇒ Object
- #get_session(env, sid) ⇒ Object
-
#initialize(app, options = {}) ⇒ Moped
constructor
A new instance of Moped.
- #set_session(env, sid, new_session, options) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Moped
Returns a new instance of Moped.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rack/session/moped.rb', line 15 def initialize(app, ={}) super @mutex = Mutex.new session = @default_options[:session] || ::Moped::Session.new(@default_options[:seeds]) session.use @default_options[:db] @pool = session[@default_options[:collection]] @pool.indexes.create(expires: -1) @pool.indexes.create({sid: 1}, {unique: true}) @marshal_data = @default_options[:marshal_data].nil? ? true : @default_options[:marshal_data] == true @next_expire_period = nil @recheck_expire_period = @default_options[:clear_expired_after].nil? ? 1800 : @default_options[:clear_expired_after].to_i end |
Instance Attribute Details
#marshal_data ⇒ Object (readonly)
Returns the value of attribute marshal_data.
7 8 9 |
# File 'lib/rack/session/moped.rb', line 7 def marshal_data @marshal_data end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
7 8 9 |
# File 'lib/rack/session/moped.rb', line 7 def mutex @mutex end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
7 8 9 |
# File 'lib/rack/session/moped.rb', line 7 def pool @pool end |
Instance Method Details
#destroy_session(env, sid, options) ⇒ Object
62 63 64 65 |
# File 'lib/rack/session/moped.rb', line 62 def destroy_session(env, sid, ) delete_session(sid) generate_sid unless [:drop] end |
#get_session(env, sid) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rack/session/moped.rb', line 28 def get_session(env, sid) @mutex.lock if env['rack.multithread'] session = find_session(sid) if sid unless sid and session env['rack.errors'].puts("Session '#{sid}' not found, initializing...") if $VERBOSE and not sid.nil? session = {} sid = generate_sid save_session(sid) end session.instance_variable_set('@old', {}.merge(session)) session.instance_variable_set('@sid', sid) return [sid, session] ensure @mutex.unlock if env['rack.multithread'] end |
#set_session(env, sid, new_session, options) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rack/session/moped.rb', line 44 def set_session(env, sid, new_session, ) @mutex.lock if env['rack.multithread'] expires = Time.now + [:expire_after] if ![:expire_after].nil? session = find_session(sid) || {} if [:renew] or [:drop] delete_session(sid) return false if [:drop] sid = generate_sid save_session(sid, session, expires) end old_session = new_session.instance_variable_get('@old') || {} session = merge_sessions(sid, old_session, new_session, session) save_session(sid, session, expires) return sid ensure @mutex.unlock if env['rack.multithread'] end |