Sha256: 198f5b2590f1ceefbb5335f9ddd6831b60f8a0348ec6f52de13e0423634c99f0

Contents?: true

Size: 1.95 KB

Versions: 5

Compression:

Stored size: 1.95 KB

Contents

require 'mongo'
require 'mongo_session_store/mongo_store_base'

module ActionDispatch
  module Session
    class MongoStore < MongoStoreBase
      class Session
        attr_accessor :_id, :data, :created_at, :updated_at

        def initialize(options = {})
          @_id        = options[:_id]
          @data       = options[:data] || BSON::Binary.new(Marshal.dump({}), :generic)
          @created_at = options[:created_at]
          @updated_at = options[:updated_at]
        end

        def self.load(options = {})
          options[:data] = options["data"] if options["data"]
          new(options)
        end

        def scope
          collection.find(:_id => _id)
        end

        def destroy
          scope.delete_one
        end

        def save
          @created_at ||= Time.now
          @updated_at   = Time.now

          attributes = {
            :data       => BSON::Binary.new(@data, :generic),
            :created_at => @created_at,
            :updated_at => @updated_at
          }

          scope.replace_one(attributes, upsert: true)
        end

        def data=(stuff)
          @data = stuff.to_s
        end

        def self.where(query = {})
          collection.find(query).map { |doc| load(doc) }
        end

        def self.last
          where.last
        end

        def self.database
          if @database
            @database
          else
            raise "MongoStore needs a database, e.g. MongoStore::Session.database = Mongo::Client.new(['127.0.0.1:27017'], database: \"my_app_development\")"
          end
        end

        def self.database=(database)
          @database = database
        end

        def self.collection
          @collection ||= database[MongoSessionStore.collection_name]
        end

        def self.reset_collection
          @collection = nil
        end

        def collection
          self.class.collection
        end
      end
    end
  end
end

MongoStore = ActionDispatch::Session::MongoStore

Version data entries

5 entries across 5 versions & 4 rubygems

Version Path
mongo_session_store-rails5-7.0.0.1 lib/mongo_session_store/mongo_store.rb
mongo_session_store-rails5-7.0.0 lib/mongo_session_store/mongo_store.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongo_session_store-rails4-6.0.0/lib/mongo_session_store/mongo_store.rb
mongo_session_store-rails3-6.0.0 lib/mongo_session_store/mongo_store.rb
mongo_session_store-rails4-6.0.0 lib/mongo_session_store/mongo_store.rb