class ActionDispatch::Request::Session

:nodoc:
Session is responsible for lazily loading the session from store.

def self.create(store, req, default_options)

Creates a session hash, merging the properties of the previous session if any.
def self.create(store, req, default_options)
  session_was = find req
  session     = Request::Session.new(store, req)
  session.merge! session_was if session_was
  set(req, session)
  Options.set(req, Request::Session::Options.new(store, default_options))
  session
end

def self.find(req)

def self.find(req)
  req.get_header ENV_SESSION_KEY
end

def self.set(req, session)

def self.set(req, session)
  req.set_header ENV_SESSION_KEY, session
end

def [](key)

+nil+ if the given key is not found in the session.
Returns value of the key stored in the session or
def [](key)
  load_for_read!
  key = key.to_s
  if key == "session_id"
    id && id.public_id
  else
    @delegate[key]
  end
end

def []=(key, value)

Writes given value to given key of the session.
def []=(key, value)
  load_for_write!
  @delegate[key.to_s] = value
end

def clear

Clears the session.
def clear
  load_for_write!
  @delegate.clear
end

def delete(key)

Deletes given key from the session.
def delete(key)
  load_for_write!
  @delegate.delete key.to_s
end

def destroy

def destroy
  clear
  options = self.options || {}
  @by.send(:delete_session, @req, options.id(@req), options)
  # Load the new sid to be written with the response.
  @loaded = false
  load_for_write!
end

def each(&block)

def each(&block)
  to_hash.each(&block)
end

def empty?

def empty?
  load_for_read!
  @delegate.empty?
end

def exists?

def exists?
  return @exists unless @exists.nil?
  @exists = @by.send(:session_exists?, @req)
end

def fetch(key, default = Unspecified, &block)

# => :bar
end
:bar
session.fetch(:foo) do

# => :bar
session.fetch(:foo, :bar)

# => KeyError: key not found: "foo"
session.fetch(:foo)

Returns default value if specified.
if can't find the given key and no default value is set.
Returns value of the given key from the session, or raises +KeyError+
def fetch(key, default = Unspecified, &block)
  load_for_read!
  if default == Unspecified
    @delegate.fetch(key.to_s, &block)
  else
    @delegate.fetch(key.to_s, default, &block)
  end
end

def has_key?(key)

Returns true if the session has the given key or false.
def has_key?(key)
  load_for_read!
  @delegate.key?(key.to_s)
end

def id

def id
  options.id(@req)
end

def initialize(by, req)

def initialize(by, req)
  @by       = by
  @req      = req
  @delegate = {}
  @loaded   = false
  @exists   = nil # We haven't checked yet.
end

def inspect

def inspect
  if loaded?
    super
  else
    "#<#{self.class}:0x#{(object_id << 1).to_s(16)} not yet loaded>"
  end
end

def keys

Returns keys of the session as Array.
def keys
  load_for_read!
  @delegate.keys
end

def load!

def load!
  id, session = @by.load_session @req
  options[:id] = id
  @delegate.replace(stringify_keys(session))
  @loaded = true
end

def load_for_read!

def load_for_read!
  load! if !loaded? && exists?
end

def load_for_write!

def load_for_write!
  load! unless loaded?
end

def loaded?

def loaded?
  @loaded
end

def merge!(other)

def merge!(other)
  load_for_write!
  @delegate.merge!(other)
end

def options

def options
  Options.find @req
end

def stringify_keys(other)

def stringify_keys(other)
  other.each_with_object({}) { |(key, value), hash|
    hash[key.to_s] = value
  }
end

def to_hash

Returns the session as Hash.
def to_hash
  load_for_read!
  @delegate.dup.delete_if { |_, v| v.nil? }
end

def update(hash)

# => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2", "foo" => "bar"}
session.to_hash

# => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2", "foo" => "bar"}
session.update({ "foo" => "bar" })

# => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2"}
session.to_hash

Updates the session with given Hash.
def update(hash)
  load_for_write!
  @delegate.update stringify_keys(hash)
end

def values

Returns values of the session as Array.
def values
  load_for_read!
  @delegate.values
end