Sha256: add66175b612c9f16df3cc1d97271ac0ab0899d17ab26cbd4fa440a70e8d0c51

Contents?: true

Size: 1.04 KB

Versions: 7

Compression:

Stored size: 1.04 KB

Contents

require "mongo_session_store/mongo_store_base"

module ActionDispatch
  module Session
    class MongoidStore < MongoStoreBase
      class Session
        include Mongoid::Document
        include Mongoid::Timestamps

        attr_writer :data

        store_in :collection => MongoSessionStore.collection_name

        field :_data, :type => BSON::Binary, :default => -> { pack({}) }

        def self.pack(data)
          BSON::Binary.new(Marshal.dump(data), :generic)
        end

        def data
          @data ||= unpack(_data)
        end

        def reload
          @data = nil
          super
        end

        private

        before_save do
          self._data = pack(data)
        end

        def pack(data)
          self.class.pack(data)
        end

        def unpack(packed)
          return unless packed
          if packed.respond_to? :data
            Marshal.load(packed.data)
          else
            Marshal.load(packed.to_s)
          end
        end
      end
    end
  end
end

MongoidStore = ActionDispatch::Session::MongoidStore

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
mongo_session_store-3.2.1 lib/mongo_session_store/mongoid_store.rb
mongo_session_store-3.2.0 lib/mongo_session_store/mongoid_store.rb
mongo_session_store-3.1.0 lib/mongo_session_store/mongoid_store.rb
mongo_session_store-3.0.0 lib/mongo_session_store/mongoid_store.rb
mongo_session_store-3.0.0.beta.1 lib/mongo_session_store/mongoid_store.rb
mongo_session_store-rails-7.0.2 lib/mongo_session_store/mongoid_store.rb
mongo_session_store-rails-7.0.1 lib/mongo_session_store/mongoid_store.rb