class Rack::Session::Abstract::SessionHash - rack-1.6.4 Documentation

# class Rack::Session::Abstract::SessionHash

SessionHash is responsible to lazily load the session from store.

### Attributes

id[W]

### Public Class Methods

find(env) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 27
    def self.find(env)
      env[ENV_SESSION_KEY]
    end

new(store, env) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 39
    def initialize(store, env)
      @store = store
      @env = env
      @loaded = false
    end

set(env, session) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 31
    def self.set(env, session)
      env[ENV_SESSION_KEY] = session
    end

set_options(env, options) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 35
    def self.set_options(env, options)
      env[ENV_SESSION_OPTIONS_KEY] = options.dup
    end

### Public Instance Methods

[](key) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 59
    def [](key)
      load_for_read!
      @data[key.to_s]
    end

Also aliased as: fetch
[]=(key, value) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 72
    def []=(key, value)
      load_for_write!
      @data[key.to_s] = value
    end

Also aliased as: store
clear() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 78
    def clear
      load_for_write!
      @data.clear
    end

delete(key) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 104
    def delete(key)
      load_for_write!
      @data.delete(key.to_s)
    end

destroy() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 83
    def destroy
      clear
      @id = @store.send(:destroy_session, @env, id, options)
    end

each(&block) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 54
    def each(&block)
      load_for_read!
      @data.each(&block)
    end

empty?() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 127
    def empty?
      load_for_read!
      @data.empty?
    end

exists?() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 117
    def exists?
      return @exists if instance_variable_defined?(:@exists)
      @data = {}
      @exists = @store.send(:session_exists?, @env)
    end

fetch(key)
Alias for: []
has_key?(key) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 65
    def has_key?(key)
      load_for_read!
      @data.has_key?(key.to_s)
    end

Also aliased as: key?, include?
id() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 45
    def id
      return @id if @loaded or instance_variable_defined?(:@id)
      @id = @store.send(:extract_session_id, @env)
    end

include?(key)
Alias for: has_key?
inspect() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 109
    def inspect
      if loaded?
        @data.inspect
      else
        "#<#{self.class}:0x#{self.object_id.to_s(16)} not yet loaded>"
      end
    end

key?(key)
Alias for: has_key?
keys() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 132
    def keys
      @data.keys
    end

loaded?() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 123
    def loaded?
      @loaded
    end

merge!(hash)
Alias for: update
options() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 50
    def options
      @env[ENV_SESSION_OPTIONS_KEY]
    end

replace(hash) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 99
    def replace(hash)
      load_for_write!
      @data.replace(stringify_keys(hash))
    end

store(key, value)
Alias for: []=
to_hash() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 88
    def to_hash
      load_for_read!
      @data.dup
    end

update(hash) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 93
    def update(hash)
      load_for_write!
      @data.update(stringify_keys(hash))
    end

Also aliased as: merge!
values() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 136
    def values
      @data.values
    end

### Private Instance Methods

load!() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 150
    def load!
      @id, session = @store.send(:load_session, @env)
      @data = stringify_keys(session)
      @loaded = true
    end

load_for_read!() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 142
    def load_for_read!
      load! if !loaded? && exists?
    end

load_for_write!() click to toggle source

    # File lib/rack/session/abstract/id.rb, line 146
    def load_for_write!
      load! unless loaded?
    end

stringify_keys(other) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 156
    def stringify_keys(other)
      hash = {}
      other.each do |key, value|
        hash[key.to_s] = value
      end
      hash
    end