Module: Camping::Session

Defined in:
lib/camping/session.rb

Overview

  • Session data is only saved if it has changed.

Instance Method Summary collapse

Instance Method Details

#service(*a) ⇒ Object

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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/camping/session.rb', line 107

def service(*a)
    session = Camping::Models::Session.persist @cookies
    app = self.class.name.gsub(/^(\w+)::.+$/, '\1')
    @state = (session[app] ||= Camping::H[])
    hash_before = Marshal.dump(@state).hash
    s = super(*a)
    if session
        hash_after = Marshal.dump(@state).hash
        unless hash_before == hash_after
            session[app] = @state
            session.save
        end
    end
    s
end