Class: ActionDispatch::Request::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/request/session.rb

Overview

Session is responsible for lazily loading the session from store.

Defined Under Namespace

Classes: Options

Constant Summary collapse

ENV_SESSION_KEY =

:nodoc:

Rack::RACK_SESSION
ENV_SESSION_OPTIONS_KEY =

:nodoc:

Rack::RACK_SESSION_OPTIONS
Unspecified =

Singleton object used to determine if an optional param wasn’t specified.

Object.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(by, req) ⇒ Session

Returns a new instance of Session.

[View source]

63
64
65
66
67
68
69
# File 'lib/action_dispatch/request/session.rb', line 63

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

Class Method Details

.create(store, req, default_options) ⇒ Object

Creates a session hash, merging the properties of the previous session if any.

[View source]

16
17
18
19
20
21
22
23
24
# File 'lib/action_dispatch/request/session.rb', line 16

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

.find(req) ⇒ Object

[View source]

26
27
28
# File 'lib/action_dispatch/request/session.rb', line 26

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

.set(req, session) ⇒ Object

[View source]

30
31
32
# File 'lib/action_dispatch/request/session.rb', line 30

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

Instance Method Details

#[](key) ⇒ Object

Returns value of the key stored in the session or nil if the given key is not found in the session.

[View source]

91
92
93
94
95
96
97
98
99
100
# File 'lib/action_dispatch/request/session.rb', line 91

def [](key)
  load_for_read!
  key = key.to_s

  if key == "session_id"
    id&.public_id
  else
    @delegate[key]
  end
end

#[]=(key, value) ⇒ Object

Writes given value to given key of the session.

[View source]

131
132
133
134
# File 'lib/action_dispatch/request/session.rb', line 131

def []=(key, value)
  load_for_write!
  @delegate[key.to_s] = value
end

#clearObject

Clears the session.

[View source]

137
138
139
140
# File 'lib/action_dispatch/request/session.rb', line 137

def clear
  load_for_write!
  @delegate.clear
end

#delete(key) ⇒ Object

Deletes given key from the session.

[View source]

165
166
167
168
# File 'lib/action_dispatch/request/session.rb', line 165

def delete(key)
  load_for_write!
  @delegate.delete key.to_s
end

#destroyObject

[View source]

79
80
81
82
83
84
85
86
87
# File 'lib/action_dispatch/request/session.rb', line 79

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

#dig(*keys) ⇒ Object

Returns the nested value specified by the sequence of keys, returning nil if any intermediate step is nil.

[View source]

104
105
106
107
108
# File 'lib/action_dispatch/request/session.rb', line 104

def dig(*keys)
  load_for_read!
  keys = keys.map.with_index { |key, i| i.zero? ? key.to_s : key }
  @delegate.dig(*keys)
end

#each(&block) ⇒ Object

[View source]

220
221
222
# File 'lib/action_dispatch/request/session.rb', line 220

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

#empty?Boolean

Returns:

  • (Boolean)
[View source]

210
211
212
213
# File 'lib/action_dispatch/request/session.rb', line 210

def empty?
  load_for_read!
  @delegate.empty?
end

#exists?Boolean

Returns:

  • (Boolean)
[View source]

201
202
203
204
# File 'lib/action_dispatch/request/session.rb', line 201

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

#fetch(key, default = Unspecified, &block) ⇒ Object

Returns value of the given key from the session, or raises KeyError if can’t find the given key and no default value is set. Returns default value if specified.

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

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

session.fetch(:foo) do
  :bar
end
# => :bar
[View source]

184
185
186
187
188
189
190
191
# File 'lib/action_dispatch/request/session.rb', line 184

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

#has_key?(key) ⇒ Boolean Also known as: key?, include?

Returns true if the session has the given key or false.

Returns:

  • (Boolean)
[View source]

111
112
113
114
# File 'lib/action_dispatch/request/session.rb', line 111

def has_key?(key)
  load_for_read!
  @delegate.key?(key.to_s)
end

#idObject

[View source]

71
72
73
# File 'lib/action_dispatch/request/session.rb', line 71

def id
  options.id(@req)
end

#inspectObject

[View source]

193
194
195
196
197
198
199
# File 'lib/action_dispatch/request/session.rb', line 193

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

#keysObject

Returns keys of the session as Array.

[View source]

119
120
121
122
# File 'lib/action_dispatch/request/session.rb', line 119

def keys
  load_for_read!
  @delegate.keys
end

#loaded?Boolean

Returns:

  • (Boolean)
[View source]

206
207
208
# File 'lib/action_dispatch/request/session.rb', line 206

def loaded?
  @loaded
end

#merge!(other) ⇒ Object

[View source]

215
216
217
218
# File 'lib/action_dispatch/request/session.rb', line 215

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

#optionsObject

[View source]

75
76
77
# File 'lib/action_dispatch/request/session.rb', line 75

def options
  Options.find @req
end

#to_hashObject Also known as: to_h

Returns the session as Hash.

[View source]

143
144
145
146
# File 'lib/action_dispatch/request/session.rb', line 143

def to_hash
  load_for_read!
  @delegate.dup.delete_if { |_, v| v.nil? }
end

#update(hash) ⇒ Object

Updates the session with given Hash.

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

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

session.to_hash
# => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2", "foo" => "bar"}
[View source]

159
160
161
162
# File 'lib/action_dispatch/request/session.rb', line 159

def update(hash)
  load_for_write!
  @delegate.update hash.stringify_keys
end

#valuesObject

Returns values of the session as Array.

[View source]

125
126
127
128
# File 'lib/action_dispatch/request/session.rb', line 125

def values
  load_for_read!
  @delegate.values
end