Class: Surikat::SessionManager
- Inherits:
-
Object
- Object
- Surikat::SessionManager
- Defined in:
- lib/surikat/session_manager.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #delete_key!(skey, key) ⇒ Object
- #destroy!(skey) ⇒ Object
-
#initialize ⇒ SessionManager
constructor
A new instance of SessionManager.
- #merge!(key, hash) ⇒ Object
Constructor Details
#initialize ⇒ SessionManager
Returns a new instance of SessionManager.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/surikat/session_manager.rb', line 3 def initialize @config = Surikat.config.app['session'] #puts "Session manager configured with #{@config.inspect}" case @config['storage'] when 'redis' @store = Redis.new url: @config['redis_url'] when 'file' @filename = @config['file'] || 'surikat_session_store' @store = Marshal.load(File.read(@filename)) rescue {} end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
18 19 20 |
# File 'lib/surikat/session_manager.rb', line 18 def config @config end |
Instance Method Details
#[](key) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/surikat/session_manager.rb', line 20 def [](key) case @config['storage'] when 'file' @store[key] when 'redis' existing = @store.get("surikat_session_key_#{key}") existing ? Marshal.load(existing) : {} end end |
#delete_key!(skey, key) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/surikat/session_manager.rb', line 68 def delete_key!(skey, key) case @config['storage'] when 'redis' redis_key = "surikat_session_key_#{key}" if existing = @store.get(redis_key) existing_object = Marshal.load(existing) new_object = existing_object.delete(key) new_data = Marshal.dump(new_object) end @store.set(redis_key, new_data) when 'file' @store[skey].delete(key) File.open(@filename, 'w') {|f| f.write Marshal.dump(@store)} end end |
#destroy!(skey) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/surikat/session_manager.rb', line 58 def destroy!(skey) case @config['storage'] when 'redis' @store.del("surikat_session_key_#{skey}") when 'file' @store.delete(skey) File.open(@filename, 'w') {|f| f.write Marshal.dump(@store)} end end |
#merge!(key, hash) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/surikat/session_manager.rb', line 30 def merge!(key, hash) return if key.nil? case @config['storage'] when 'redis' redis_key = "surikat_session_key_#{key}" if existing = @store.get(redis_key) existing_object = Marshal.load(existing) new_object = existing_object.merge(hash) else new_object = hash end new_data = Marshal.dump(new_object) @store.set(redis_key, new_data) when 'file' if @store[key] @store[key].merge!(hash) else @store[key] = hash end File.open(@filename, 'w') {|f| f.write Marshal.dump(@store)} end true end |