Class: Rack::Session::Sequel

Inherits:
Abstract::ID
  • Object
show all
Defined in:
lib/rack/session/sequel.rb,
lib/rack/session/sequel_org.rb,
lib/rack-session-sequel/version.rb

Constant Summary collapse

DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge \
:table_name => :sessions, :db_uri => 'sqlite:/'
VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Sequel.



13
14
15
16
17
18
19
# File 'lib/rack/session/sequel.rb', line 13

def initialize(app, options={})
  options = {:db => options } if options.kind_of? ::Sequel::Database
  options = {:db_uri => options } if options.is_a? String
  super
  @pool = setup_database[@default_options[:table_name]]
  @mutex = Mutex.new
end

Instance Attribute Details

#mutexObject (readonly)

Returns the value of attribute mutex.



8
9
10
# File 'lib/rack/session/sequel.rb', line 8

def mutex
  @mutex
end

#poolObject (readonly)

Returns the value of attribute pool.



8
9
10
# File 'lib/rack/session/sequel.rb', line 8

def pool
  @pool
end

Instance Method Details

#destroy_session(env, session_id, options) ⇒ Object



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

def destroy_session(env, session_id, options)
  with_lock(env) do
    _delete(session_id)
    generate_sid unless options[:drop]
  end
end

#generate_sidObject



21
22
23
24
25
26
# File 'lib/rack/session/sequel.rb', line 21

def generate_sid
  loop do
    sid = super
    break sid unless _exists? sid
  end
end

#get_session(env, sid) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/rack/session/sequel.rb', line 28

def get_session(env, sid)
  with_lock(env, [nil, {}]) do
    unless sid and session = _get(sid)
      sid, session = generate_sid, {}
      _put sid, session
    end
    [sid, session]
  end
end

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



38
39
40
41
42
43
# File 'lib/rack/session/sequel.rb', line 38

def set_session(env, session_id, new_session, options)
  with_lock(env, false) do
    _put session_id, new_session
    session_id
  end
end

#with_lock(env, default = nil) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/rack/session/sequel.rb', line 52

def with_lock(env, default=nil)
  @mutex.lock if env['rack.multithread']
  yield
rescue
  default
ensure
  @mutex.unlock if @mutex.locked?
end