Class: ActionDispatch::Request::Session

Inherits:
Object
  • Object
show all
Defined in:
actionpack/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::Session::Abstract::ENV_SESSION_KEY
ENV_SESSION_OPTIONS_KEY =

:nodoc:

Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY
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, env) ⇒ Session


61
62
63
64
65
66
67
# File 'actionpack/lib/action_dispatch/request/session.rb', line 61

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

Class Method Details

.create(store, env, default_options) ⇒ Object


13
14
15
16
17
18
19
20
21
# File 'actionpack/lib/action_dispatch/request/session.rb', line 13

def self.create(store, env, default_options)
  session_was = find env
  session     = Request::Session.new(store, env)
  session.merge! session_was if session_was

  set(env, session)
  Options.set(env, Request::Session::Options.new(store, env, default_options))
  session
end

.find(env) ⇒ Object


23
24
25
# File 'actionpack/lib/action_dispatch/request/session.rb', line 23

def self.find(env)
  env[ENV_SESSION_KEY]
end

.set(env, session) ⇒ Object


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

def self.set(env, session)
  env[ENV_SESSION_KEY] = session
end

Instance Method Details

#[](key) ⇒ Object


88
89
90
91
# File 'actionpack/lib/action_dispatch/request/session.rb', line 88

def [](key)
  load_for_read!
  @delegate[key.to_s]
end

#[]=(key, value) ⇒ Object


108
109
110
111
# File 'actionpack/lib/action_dispatch/request/session.rb', line 108

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

#clearObject


113
114
115
116
# File 'actionpack/lib/action_dispatch/request/session.rb', line 113

def clear
  load_for_write!
  @delegate.clear
end

#delete(key) ⇒ Object


128
129
130
131
# File 'actionpack/lib/action_dispatch/request/session.rb', line 128

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

#destroyObject


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

def destroy
  clear
  options = self.options || {}
  new_sid = @by.send(:destroy_session, @env, options[:id], options)
  options[:id] = new_sid # Reset session id with a new value or nil

  # Load the new sid to be written with the response
  @loaded = false
  load_for_write!
end

#empty?Boolean


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

def empty?
  load_for_read!
  @delegate.empty?
end

#exists?Boolean


150
151
152
153
# File 'actionpack/lib/action_dispatch/request/session.rb', line 150

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

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


133
134
135
136
137
138
139
140
# File 'actionpack/lib/action_dispatch/request/session.rb', line 133

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?


93
94
95
96
# File 'actionpack/lib/action_dispatch/request/session.rb', line 93

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

#idObject


69
70
71
# File 'actionpack/lib/action_dispatch/request/session.rb', line 69

def id
  options[:id]
end

#inspectObject


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

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

#keysObject


100
101
102
# File 'actionpack/lib/action_dispatch/request/session.rb', line 100

def keys
  @delegate.keys
end

#loaded?Boolean


155
156
157
# File 'actionpack/lib/action_dispatch/request/session.rb', line 155

def loaded?
  @loaded
end

#merge!(other) ⇒ Object


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

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

#optionsObject


73
74
75
# File 'actionpack/lib/action_dispatch/request/session.rb', line 73

def options
  Options.find @env
end

#to_hashObject


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

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

#update(hash) ⇒ Object


123
124
125
126
# File 'actionpack/lib/action_dispatch/request/session.rb', line 123

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

#valuesObject


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

def values
  @delegate.values
end