Sha256: 0778d6b0f7d6a77be8dc67eb1293129c0c55c745ec15b2305527af50d335bb91

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require 'og'
require 'nitro/session'
require 'base64'

module Nitro

  class OgSession < Session
    prop_accessor :sessionid, String
    prop_accessor :expires, Time
    prop_accessor :content
  end

  # A Session manager that persists sessions on an Og store.
  
  class OgSessionStore

    def []=(k,v)
      unless s = OgSession.find_by_sessionid(k.to_s)
        s = OgSession.new
        s.sessionid = k.to_s
      end
      #s.content = v.to_yaml
      s.content = encode(v)
      s.save
    end

    def [](k)
      s = OgSession.find_by_sessionid(k.to_s) and decode(s.content)
    end

    def gc!
      now = Og.manager.store.quote(Time.now)
      OgSession.find(:condition => "expires < #{now}").each {|s| s.delete }
    end

    def all
      OgSession.all
    end

    private

    def encode(c)
      Base64.encode64(Marshal.dump(c))
    end

    def decode(c)
      Marshal::load(Base64.decode64(c)) 
      #s.content = YAML::load(s.content)
    end

  end

  Session.store = OgSessionStore.new
end

# * Guillaume Pierronnet <guillaume.pierronnet@gmail.com>

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.26.0 lib/nitro/session/og.rb
nitro-0.27.0 lib/nitro/session/og.rb