Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from aviator/feature/create
Browse files Browse the repository at this point in the history
  • Loading branch information
relaxdiego committed Jan 2, 2014
2 parents 8995f40 + 57c97f5 commit 6c69e90
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
29 changes: 15 additions & 14 deletions lib/aviator/session_pool/session_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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.
Expand All @@ -62,25 +73,15 @@ 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


# Not thread safe! BUT good enough for
# a single-threaded web application.
def set_current(key)
raise SessionNotFoundError.new(key) unless self.get(key)

@current_key = key
end

Expand Down
45 changes: 29 additions & 16 deletions test/aviator/session_pool/session_pool_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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'

Expand All @@ -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

Expand Down

0 comments on commit 6c69e90

Please sign in to comment.