Sha256: 3031291fd19cbabee1d86f74cd3c999f4840fe941fb8789e2dd8eb0e9b781988

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

require 'active_record'
module Merb
  module SessionMixin

    def setup_session
      MERB_LOGGER.info("Setting up session")
      @session = Merb::Session.persist(cookies)
      @fingerprint_before = Marshal.dump(@session).hash
    end

    def finalize_session
      MERB_LOGGER.info("Finalize session")
      unless Marshal.dump(@session).hash == @fingerprint_before
        @session.save
      end
      @headers['Set-Cookie'] = @cookies.map { |k,v| "#{k}=#{escape(v)}; path=/" if v != @k[k] } - [nil]
    end 

    # accessor for @session. Please use session and 
    # never @session directly.    
    def session
      @session  
    end

  end  


  class Session < ::ActiveRecord::Base
    serialize :data
    
    def []=(k, v) # :nodoc:
        self.data[k] = v
    end
    def [](k) # :nodoc:
        self.data[k] rescue nil
    end
      
    RAND_CHARS = [*'A'..'Z'] + [*'0'..'9'] + [*'a'..'z']
    
    # Generates a new session ID and creates a row for the new session in the database.
    def self.generate cookies
      rand_max = RAND_CHARS.size
      sid = (0...32).inject("") { |ret,_| ret << RAND_CHARS[rand(rand_max)] }
      sess = Merb::Session.create(:sess_id => sid, :data => MerbHash[])
      cookies[:sess_id] = sess.sess_id
      sess
    end
    
    # Gets the existing session based on the <tt>camping_sid</tt> available in cookies.
    # If none is found, generates a new session.
    def self.persist cookies
      if cookies[:sess_id]
        session = Merb::Session.find_by_sess_id cookies[:sess_id]
      end
      unless session
        session = Merb::Session.generate cookies
      end
      session
    end
  end

end  


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
merb-0.0.7 lib/merb/session/merb_session.rb