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

`SessionHash` is responsible to lazily load the session from store.

### Constants

Unspecified

### Attributes

id[W]

### Public Class Methods

find(req) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 50
    def self.find(req)
      req.get_header RACK_SESSION
    end

new(store, req) click to toggle source

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

set(req, session) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 54
    def self.set(req, session)
      req.set_header RACK_SESSION, session
    end

set_options(req, options) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 58
    def self.set_options(req, options)
      req.set_header RACK_SESSION_OPTIONS, options.dup
    end

### Public Instance Methods

[](key) click to toggle source

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

[]=(key, value) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 108
    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 114
    def clear
      load_for_write!
      @data.clear
    end

delete(key) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 140
    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 119
    def destroy
      clear
      @id = @store.send(:delete_session, @req, id, options)
    end

dig(key, *keys) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 87
    def dig(key, *keys)
      load_for_read!
      @data.dig(key.to_s, *keys)
    end

each(&block) click to toggle source

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

empty?() click to toggle source

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

exists?() click to toggle source

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

fetch(key, default = Unspecified, &block) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 92
    def fetch(key, default = Unspecified, &block)
      load_for_read!
      if default == Unspecified
        @data.fetch(key.to_s, &block)
      else
        @data.fetch(key.to_s, default, &block)
      end
    end

has_key?(key) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 101
    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 68
    def id
      return @id if @loaded or instance_variable_defined?(:@id)
      @id = @store.send(:extract_session_id, @req)
    end

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

    # File lib/rack/session/abstract/id.rb, line 145
    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 168
    def keys
      load_for_read!
      @data.keys
    end

loaded?() click to toggle source

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

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

    # File lib/rack/session/abstract/id.rb, line 73
    def options
      @req.session_options
    end

replace(hash) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 135
    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 124
    def to_hash
      load_for_read!
      @data.dup
    end

update(hash) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 129
    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 173
    def values
      load_for_read!
      @data.values
    end

### Private Instance Methods

load!() click to toggle source

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

load_for_read!() click to toggle source

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

load_for_write!() click to toggle source

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

stringify_keys(other) click to toggle source

    # File lib/rack/session/abstract/id.rb, line 194
    def stringify_keys(other)
      # Use transform_keys after dropping Ruby 2.4 support
      hash = {}
      other.to_hash.each do |key, value|
        hash[key.to_s] = value
      end
      hash
    end