class ActionDispatch::Request::Session

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/action_dispatch/request/session.rbs

class ActionDispatch::Request::Session
  def self.find: (ActionDispatch::Request req) -> nil
  def []: (String key) -> String
  def enabled?: () -> true
  def has_key?: (String key) -> true
  def id: () -> Rack::Session::SessionId
  def initialize: (ActionDispatch::Session::CookieStore by, ActionDispatch::Request req, enabled: true) -> void
  def load!: () -> untyped
  def load_for_read!: () -> true?
  def loaded?: () -> bool
  def options: () -> ActionDispatch::Request::Session::Options
end

: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.delete(req)

def self.delete(req)
  req.delete_header ENV_SESSION_KEY
end

def self.disabled(req)

def self.disabled(req)
  new(nil, req, enabled: false).tap do
    Session::Options.set(req, Session::Options.new(nil, { id: nil }))
  end
end

def self.find(req)

Experimental RBS support (using type sampling data from the type_fusion project).

def self.find: (ActionDispatch::Request req) -> nil

This signature was generated using 1 sample from 1 application.

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)

Experimental RBS support (using type sampling data from the type_fusion project).

def []: (String key) -> String

This signature was generated using 1 sample from 1 application.

+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&.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_delete!
  @delegate.clear
end

def delete(key)

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

def destroy

def destroy
  clear
  if enabled?
    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
end

def dig(*keys)

+nil+ if any intermediate step is +nil+.
Returns the nested value specified by the sequence of keys, returning
def dig(*keys)
  load_for_read!
  keys = keys.map.with_index { |key, i| i.zero? ? key.to_s : key }
  @delegate.dig(*keys)
end

def each(&block)

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

def empty?

def empty?
  load_for_read!
  @delegate.empty?
end

def enabled?

Experimental RBS support (using type sampling data from the type_fusion project).

def enabled?: () -> true

This signature was generated using 4 samples from 1 application.

def enabled?
  @enabled
end

def exists?

def exists?
  return false unless enabled?
  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)

Experimental RBS support (using type sampling data from the type_fusion project).

def has_key?: (String key) -> true

This signature was generated using 1 sample from 1 application.

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

Experimental RBS support (using type sampling data from the type_fusion project).

def id: () -> Rack::Session::SessionId

This signature was generated using 1 sample from 1 application.

def id
  options.id(@req)
end

def initialize(by, req, enabled: true)

Experimental RBS support (using type sampling data from the type_fusion project).

def initialize: (ActionDispatch::Session::CookieStore by, ActionDispatch::Request req, enabled: true) -> void

This signature was generated using 1 sample from 1 application.

def initialize(by, req, enabled: true)
  @by       = by
  @req      = req
  @delegate = {}
  @loaded   = false
  @exists   = nil # We haven't checked yet.
  @enabled  = enabled
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!

Experimental RBS support (using type sampling data from the type_fusion project).

def load!: () -> untyped

This signature was generated using 1 sample from 1 application.

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

def load_for_delete!

def load_for_delete!
  load! if enabled? && !loaded?
end

def load_for_read!

Experimental RBS support (using type sampling data from the type_fusion project).

def load_for_read!: () -> true?

This signature was generated using 2 samples from 1 application.

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

def load_for_write!

def load_for_write!
  if enabled?
    load! unless loaded?
  else
    raise DisabledSessionError, "Your application has sessions disabled. To write to the session you must first configure a session store"
  end
end

def loaded?

Experimental RBS support (using type sampling data from the type_fusion project).

def loaded?: () -> bool

This signature was generated using 4 samples from 1 application.

def loaded?
  @loaded
end

def merge!(other)

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

def options

Experimental RBS support (using type sampling data from the type_fusion project).

def options: () -> ActionDispatch::Request::Session::Options

This signature was generated using 1 sample from 1 application.

def options
  Options.find @req
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 hash.stringify_keys
end

def values

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