Kiss A simpler, smarter web application framework for Ruby

Kiss::SequelSession Subclass of Object

Description

Session store using Sequel.

Attributes

data

Not documented

Public Class Methods

generate(controller) source

Generates a new session ID and creates a row for the new session in the database.

    # File lib/kiss/sequel_session.rb, line 56
      def generate(controller)
        new(Sequel::Model(controller.db[:sessions]).create(
          :session_id => rand_uuid,
          :data => marshal({}),
          :created_at => Time.now,
          :updated_at => Time.now
        ))
      end
    
marshal(data) source

good

    # File lib/kiss/sequel_session.rb, line 85
      def marshal(data)
        Base64.encode64(Marshal.dump(data)) if data
      end
    
new(object) source

Not documented

    # File lib/kiss/sequel_session.rb, line 95
    def initialize(object)
      @object = object
      @data = self.class.unmarshal(@object[:data]) || {}
    end
    
persist(controller, session_id) source

Gets the existing session based on the session_id available in cookies. If none is found, generates a new session.

    # File lib/kiss/sequel_session.rb, line 67
      def persist(controller, session_id)
        if session_id
          if object = Sequel::Model(controller.db[:sessions]).where(:session_id => session_id).first
            session = new( object )
          end
        end
        unless session
          session = generate( controller )
        end
        session
      end
    
rand_uuid() source

Not documented

    # File lib/kiss/sequel_session.rb, line 42
      def rand_uuid
        values = [
          rand(0x0010000),
          rand(0x0010000),
          rand(0x0010000),
          rand(0x0010000),
          rand(0x0010000),
          rand(0x1000000),
          rand(0x1000000),
        ]
        "%04x%04x%04x%04x%04x%06x%06x" % values
      end
    
setup_storage(controller) source

Not documented

    # File lib/kiss/sequel_session.rb, line 18
      def setup_storage(controller)
        unless controller.db.table_exists?(:sessions)
          controller.db.create_table :sessions do
            primary_key :id, :null => false
            column :session_id, :varchar, :null => false
            column :data, :text
            column :created_at, :timestamp, :null => false
            column :updated_at, :timestamp, :null => false
            index :session_id
          end
      
          # db << <<-EOT
          #   CREATE TABLE `sessions` (
          #     `id` int(11) NOT NULL auto_increment,
          #     `session_id` varchar(255) default NULL,
          #     `data` text,
          #     `created_at` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
          #     `updated_at` timestamp NOT NULL default '0000-00-00 00:00:00',
          #     PRIMARY KEY (`id`)
          #   );
          #   EOT
        end
      end
    
unmarshal(data) source

good

    # File lib/kiss/sequel_session.rb, line 90
      def unmarshal(data)
        Marshal.load(Base64.decode64(data)) if data
      end
    

Public Instance Methods

[](key) source

good

    # File lib/kiss/sequel_session.rb, line 123
    def [](key)
      data[key]
    end
    
[]=(key, val) source

good

    # File lib/kiss/sequel_session.rb, line 128
    def []=(key, val)
      data[key] = val
    end
    
data() source

good Lazy-unmarshal session state.

    # File lib/kiss/sequel_session.rb, line 144
    def data
      @data
    end
    
delete(key = nil) source

good Lazy-delete of session data

    # File lib/kiss/sequel_session.rb, line 118
    def delete(key = nil)
      key ? self.data.delete(key) : self.data.clear
    end
    
each(&b) source

good

    # File lib/kiss/sequel_session.rb, line 138
    def each(&b)
      data.each(&b)
    end
    
empty?() source

good

    # File lib/kiss/sequel_session.rb, line 133
    def empty?
      data.empty?
    end
    
loaded?() source

good Has the session been loaded yet?

    # File lib/kiss/sequel_session.rb, line 150
    def loaded?
      !! @data
    end
    
refresh_expiration() source

Recreates the cookie with the default expiration time Useful during log in for pushing back the expiration date

    # File lib/kiss/sequel_session.rb, line 112
    def refresh_expiration
      @needs_new_cookie = true
    end
    
regenerate() source

Regenerate the Session ID

    # File lib/kiss/sequel_session.rb, line 105
    def regenerate
      @object.update_attributes(:session_id => Kiss::SessionMixin::rand_uuid)
      @needs_new_cookie = true
    end
    
save() source

good

    # File lib/kiss/sequel_session.rb, line 155
    def save
      @object[:data] = self.class.marshal(self.data)
      @object[:updated_at] = Time.now
      @object.save
    end
    
values(*args) source

Not documented

    # File lib/kiss/sequel_session.rb, line 100
    def values(*args)
      @object.values(*args)
    end
    

Disabled; run with $DEBUG to generate this.