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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(by, env) ⇒ Session

Returns a new instance of Session


58
59
60
61
62
63
64
# File 'actionpack/lib/action_dispatch/request/session.rb', line 58

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


10
11
12
13
14
15
16
17
18
# File 'actionpack/lib/action_dispatch/request/session.rb', line 10

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


20
21
22
# File 'actionpack/lib/action_dispatch/request/session.rb', line 20

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

.set(env, session) ⇒ Object


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

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

Instance Method Details

#[](key) ⇒ Object


85
86
87
88
# File 'actionpack/lib/action_dispatch/request/session.rb', line 85

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

#[]=(key, value) ⇒ Object


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

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

#clearObject


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

def clear
  load_for_write!
  @delegate.clear
end

#delete(key) ⇒ Object


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

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

#destroyObject


74
75
76
77
78
79
80
81
82
83
# File 'actionpack/lib/action_dispatch/request/session.rb', line 74

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

Returns:

  • (Boolean)

147
148
149
150
# File 'actionpack/lib/action_dispatch/request/session.rb', line 147

def empty?
  load_for_read!
  @delegate.empty?
end

#exists?Boolean

Returns:

  • (Boolean)

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

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

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

Returns:

  • (Boolean)

90
91
92
93
# File 'actionpack/lib/action_dispatch/request/session.rb', line 90

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

#idObject


66
67
68
# File 'actionpack/lib/action_dispatch/request/session.rb', line 66

def id
  options[:id]
end

#inspectObject


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

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

#keysObject


97
98
99
# File 'actionpack/lib/action_dispatch/request/session.rb', line 97

def keys
  @delegate.keys
end

#loaded?Boolean

Returns:

  • (Boolean)

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

def loaded?
  @loaded
end

#merge!(other) ⇒ Object


152
153
154
155
# File 'actionpack/lib/action_dispatch/request/session.rb', line 152

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

#optionsObject


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

def options
  Options.find @env
end

#to_hashObject


115
116
117
118
# File 'actionpack/lib/action_dispatch/request/session.rb', line 115

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

#update(hash) ⇒ Object


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

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

#valuesObject


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

def values
  @delegate.values
end