Module Camping::Session
In: lib/camping/session.rb

The Camping::Session module is designed to be mixed into your application or into specific controllers which require sessions. This module defines a service method which intercepts all requests handed to those controllers.

Getting Started

To get sessions working for your application:

  1. require ‘camping/session‘
  2. Mixin the module: module YourApp; include Camping::Session end
  3. In your application‘s create method, add a call to Camping::Models::Session.create_schema
  4. Throughout your application, use the @state var like a hash to store your application‘s data.

If you are unfamiliar with the create method, see code.whytheluckystiff.net/camping/wiki/GiveUsTheCreateMethod.

A Few Notes

  • The session ID is stored in a cookie. Look in @cookies.camping_sid.
  • The session data is stored in the sessions table in your database.
  • All mounted Camping apps using this class will use the same database table.
  • However, your application‘s data is stored in its own hash.
  • Session data is only saved if it has changed.

Methods

service  

Public Instance methods

This service method, when mixed into controllers, intercepts requests and wraps them with code to start and close the session. If a session isn‘t found in the database it is created. The @state variable is set and if it changes, it is saved back into the database.

[Source]

     # File lib/camping/session.rb, line 107
107:     def service(*a)
108:         session = Camping::Models::Session.persist @cookies
109:         app = self.class.name.gsub(/^(\w+)::.+$/, '\1')
110:         @state = (session[app] ||= Camping::H[])
111:         hash_before = Marshal.dump(@state).hash
112:         s = super(*a)
113:         if session
114:             hash_after = Marshal.dump(@state).hash
115:             unless hash_before == hash_after
116:                 session[app] = @state
117:                 session.save
118:             end
119:         end
120:         s
121:     end

[Validate]