Class: Rack::Session::Sequel
- Inherits:
-
Abstract::ID
- Object
- Abstract::ID
- Rack::Session::Sequel
- 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
-
#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, session_id, options) ⇒ Object
- #generate_sid ⇒ Object
- #get_session(env, sid) ⇒ Object
-
#initialize(app, options = {}) ⇒ Sequel
constructor
A new instance of Sequel.
- #set_session(env, session_id, new_session, options) ⇒ Object
- #with_lock(env, default = nil) ⇒ Object
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, ={}) = {:db => } if .kind_of? ::Sequel::Database = {:db_uri => } if .is_a? String super @pool = setup_database[@default_options[:table_name]] @mutex = Mutex.new end |
Instance Attribute Details
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
8 9 10 |
# File 'lib/rack/session/sequel.rb', line 8 def mutex @mutex end |
#pool ⇒ Object (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, ) with_lock(env) do _delete(session_id) generate_sid unless [:drop] end end |
#generate_sid ⇒ Object
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, ) 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 |