Sha256: 335e061163374e0d0642399a07b59ad045f7c149be8e5bd33461f8cf1a918906

Contents?: true

Size: 1.79 KB

Versions: 6

Compression:

Stored size: 1.79 KB

Contents

require 'merb-core/dispatch/session'

module Merb
  class DataMapperSessionStore
    include ::DataMapper::Resource

    def self.default_repository_name
      Merb::Plugins.config[:merb_datamapper][:session_repository_name]
    end

    storage_names[default_repository_name] = Merb::Plugins.config[:merb_datamapper][:session_storage_name]

    property :session_id, String, :length => 32, :required => true, :key => true
    property :data, Object, :default => {}, :lazy => false
    property :created_at, DateTime, :default => Proc.new { |r, p| DateTime.now }

    ##
    # Retrieves a session from the session store
    #
    # @param session_id<String> The session_id to retrieve the session for
    #
    # @returns <nil, DataMapperSessionStore> The session corresponding to the id, or nil
    def self.retrieve_session(session_id)
      if session = get(session_id)
        session.data
      end
    end

    ##
    # Stores the data in a session with the given session_id, creating it if
    # required
    #
    # @param session_id<String> The session_id to find the session by, or the id of the new session
    # @param data<Object> The data to be stored in the session. Probably a hash
    def self.store_session(session_id, data)
      if session = get(session_id)
        session.update(:data => data)
      else
        create(:session_id => session_id, :data => data)
      end
    end

    ##
    # Deletes a session with the given id
    #
    # @param session_id<String> The session to destroy
    def self.delete_session(session_id)
      all(:session_id => session_id).destroy!
    end
  end

  class DataMapperSession < SessionStoreContainer

    # The session store type
    self.session_store_type = :datamapper

    # The store object is the model class itself
    self.store = DataMapperSessionStore
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
merb_datamapper-1.1.3 lib/merb_datamapper/data_mapper_session.rb
merb_datamapper-1.1.2 lib/merb_datamapper/data_mapper_session.rb
merb_datamapper-1.1.1 lib/merb_datamapper/data_mapper_session.rb
merb_datamapper-1.1.0 lib/merb_datamapper/data_mapper_session.rb
merb_datamapper-1.1.0.rc1 lib/merb_datamapper/data_mapper_session.rb
merb_datamapper-1.1.0.pre lib/merb_datamapper/data_mapper_session.rb