Class: Rack::Session::Moped

Inherits:
Abstract::ID
  • Object
show all
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

Instance Method Summary collapse

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, options={})
  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_dataObject (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

#mutexObject (readonly)

Returns the value of attribute mutex.



7
8
9
# File 'lib/rack/session/moped.rb', line 7

def mutex
  @mutex
end

#poolObject (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, options)
  delete_session(sid)
  generate_sid unless options[: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, options)
  @mutex.lock if env['rack.multithread']
  expires = Time.now + options[:expire_after] if !options[:expire_after].nil?
  session = find_session(sid) || {}
  if options[:renew] or options[:drop]
    delete_session(sid)
    return false if options[: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