Sha256: d98eeb86a408fc69dc5ee116f82ec46e4b7eb53e47ae5a1d29cad797b81db0aa

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'dm-core'

module ActionController
  module Session
    class DatamapperStore < AbstractStore

      def initialize(app, options = {})
        super
        if options.delete(:cache)
          @@cache = {}
        end
        @@session_class = ::DatamapperStore::Session
      end
      
      private
      def get_session(env, sid)
        sid ||= generate_sid
        session = 
          if @@cache
            @@cache[sid] || @@session_class.get(sid)
          else
            @@session_class.get(sid)
          end
        [sid, session.nil? ? {} : session.data]
      end
      
      def set_session(env, sid, session_data)
        session = 
          if @@cache
            @@cache[sid] || @@session_class.get(sid)
          else
            @@session_class.get(sid)
          end || @@session_class.new(:session_id => sid)
        session.data = session_data || {}
        if session.new_record?
          session.updated_at = Time.now
          @@cache[sid] = session if @@cache
        end
        session.save
      end
    end
  end
end

module DatamapperStore
  class Session
    
    include ::DataMapper::Resource
    
    def self.name
      "session"
    end
    
    property :session_id, String, :key => true
    
    property :data, Text, :nullable => false, :default => ::Base64.encode64(Marshal.dump({}))
    
    property :updated_at, DateTime, :nullable => true, :index => true
    
    def data=(data)
      attribute_set(:data, ::Base64.encode64(Marshal.dump(data)))
    end
    def data
      Marshal.load(::Base64.decode64(attribute_get(:data)))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datamapper4rail-0.1.0 lib/datamapper4rails/datamapper_store.rb