Class: Aviator::SessionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/aviator/session_pool/version.rb,
lib/aviator/session_pool/session_pool.rb

Defined Under Namespace

Classes: CurrentSessionNotDefinedError, SessionNotFoundError

Constant Summary collapse

VERSION =
"0.0.1"
REDIS_KEY_PREFIX =
'aviator.session_dumps'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject (readonly) Also known as: c

Returns the value of attribute configuration.



51
52
53
# File 'lib/aviator/session_pool/session_pool.rb', line 51

def configuration
  @configuration
end

Class Method Details

.[](key) ⇒ Object Also known as: get



30
31
32
33
34
35
36
37
38
# File 'lib/aviator/session_pool/session_pool.rb', line 30

def [](key)
  session_dump = redis.get(build_key(key))

  return nil unless session_dump

  session = Aviator::Session.load(session_dump)

  session.validate ? session : nil
end

.[]=(key, session) ⇒ Object



23
24
25
26
27
# File 'lib/aviator/session_pool/session_pool.rb', line 23

def []=(key, session)
  session_key = build_key(key)

  redis.set(session_key, session.dump)
end

.configure(options) ⇒ Object

Not thread safe! BUT good enough for a single-threaded web application.



44
45
46
47
48
49
50
# File 'lib/aviator/session_pool/session_pool.rb', line 44

def configure(options)
  @configuration = options

  # So that the redis configuration will
  # be reloaded on the next ::redis call
  @redis = nil
end

.get_currentObject

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.



58
59
60
# File 'lib/aviator/session_pool/session_pool.rb', line 58

def get_current
  self.get(@current_key) || (raise CurrentSessionNotDefinedError.new)
end

.get_or_create(key, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aviator/session_pool/session_pool.rb', line 63

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
end

.set_current(key) ⇒ Object

Not thread safe! BUT good enough for a single-threaded web application.



81
82
83
84
85
# File 'lib/aviator/session_pool/session_pool.rb', line 81

def set_current(key)
  raise SessionNotFoundError.new(key) unless self.get(key)
  
  @current_key = key
end