Sha256: 6914e3d4451a5429cd8febf0a14544aa5d359739d2e04e6e271585b4b9e7c5a1

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require 'couchrest'
require 'couchrest_model'

# CouchDB session storage for Rails.
#
# It will automatically pick up the config/couch.yml file for CouchRest Model
#
# Options:
# :database => database to use combined with config prefix and suffix
#
class CouchRestSessionStore < ActionDispatch::Session::AbstractStore

  include CouchRest::Model::Configuration
  include CouchRest::Model::Connection

  class << self
    def marshal(data)
      ::Base64.encode64(Marshal.dump(data)) if data
    end

    def unmarshal(data)
      Marshal.load(::Base64.decode64(data)) if data
    end

  end

  def initialize(app, options = {})
    super
    self.class.use_database options[:database] || "sessions"
  end

  # just fetch from the config
  def self.database
    @database ||= prepare_database
  end

  def database
    self.class.database
  end

  private

  def get_session(env, sid)
    debugger
    if sid
      doc = database.get(sid)
      session = self.class.unmarshal(doc["data"])
    else
      sid = generate_sid
      session = {}
      doc = CouchRest::Document.new "_id" => sid,
        "data" => self.class.marshal(session)
      database.save_doc(doc)
    end
    return [sid, session]
  end

  def set_session(env, sid, session, options)
    doc = database.get(sid)
    doc["data"] = self.class.marshal(session)
    database.save_doc(doc)
    return sid
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
couchrest_session_store-0.0.1 lib/couchrest_session_store.rb