diff --git a/lib/aviator/session_pool/session_pool.rb b/lib/aviator/session_pool/session_pool.rb index b262f89..f921fa3 100644 --- a/lib/aviator/session_pool/session_pool.rb +++ b/lib/aviator/session_pool/session_pool.rb @@ -8,13 +8,13 @@ def initialize super "Current session is not defined. Make sure to call ::set_current first." end end - + class SessionNotFoundError < StandardError def initialize(key) super "There is no session with key #{ key } in the pool" end end - + REDIS_KEY_PREFIX = 'aviator.session_dumps' @@ -52,6 +52,17 @@ def configure(options) alias :c :configuration + def create(key, &block) + config = configuration.dup + [:redis_host, :redis_port].each{|k| config.delete k } + session = Aviator::Session.new(config) + + session.authenticate &block + + self[key] = session + end + + # WARNING: Since get_current uses a class instance variable, it will contain # a value between http requests whether set_current was called or not for as long # as it was called at least once. @@ -62,17 +73,7 @@ def get_current def get_or_create(key, &block) # If session is invalid or does not exist, self[] will return nil - unless session = self[key] - config = configuration.dup - [:redis_host, :redis_port].each{|k| config.delete k } - session = Aviator::Session.new(config) - - session.authenticate &block - - self[key] = session - end - - session + self.get(key) || self.create(key, &block) end @@ -80,7 +81,7 @@ def get_or_create(key, &block) # a single-threaded web application. def set_current(key) raise SessionNotFoundError.new(key) unless self.get(key) - + @current_key = key end diff --git a/test/aviator/session_pool/session_pool_test.rb b/test/aviator/session_pool/session_pool_test.rb index 14acc2e..c2d776f 100644 --- a/test/aviator/session_pool/session_pool_test.rb +++ b/test/aviator/session_pool/session_pool_test.rb @@ -111,6 +111,19 @@ def validate end # describe '::configure' + describe '::create' do + + it 'creates the session when called' do + key = 'loadsessionkey' + + subject[key] = session + + subject.create(key).dump.wont_equal session.dump + end + + end # describe '::create' + + describe '::get' do it 'aliases ::[]' do @@ -124,6 +137,19 @@ def validate end # describe '::get' + describe '::get_current' do + + it 'raises an error if set_current was no previously called' do + the_method = lambda do + subject.get_current + end + + the_method.must_raise Aviator::SessionPool::CurrentSessionNotDefinedError + end + + end # describe '::get_current' + + describe '::get_or_create' do it 'loads a session if the associated session dump exists' do @@ -193,8 +219,8 @@ def validate subject.get_current.dump.must_equal s.dump end - - + + it 'raises an error when the key does not exist' do key = 'setcurrentnonexistentsessionkey' @@ -205,20 +231,7 @@ def validate the_method.must_raise Aviator::SessionPool::SessionNotFoundError end - end - - - describe '::get_current' do - - it 'raises an error if set_current was no previously called' do - the_method = lambda do - subject.get_current - end - - the_method.must_raise Aviator::SessionPool::CurrentSessionNotDefinedError - end - - end + end # describe '::set_current' end