Class: Warden::SessionSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/warden/session_serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ SessionSerializer

Returns a new instance of SessionSerializer.



7
8
9
# File 'lib/warden/session_serializer.rb', line 7

def initialize(env)
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/warden/session_serializer.rb', line 5

def env
  @env
end

Instance Method Details

#delete(scope, user = nil) ⇒ Object



44
45
46
# File 'lib/warden/session_serializer.rb', line 44

def delete(scope, user=nil)
  session.delete(key_for(scope))
end

#deserialize(key) ⇒ Object



19
20
21
# File 'lib/warden/session_serializer.rb', line 19

def deserialize(key)
  key
end

#fetch(scope) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/warden/session_serializer.rb', line 30

def fetch(scope)
  key = session[key_for(scope)]
  return nil unless key

  method_name = "#{scope}_deserialize"
  user = respond_to?(method_name) ? send(method_name, key) : deserialize(key)
  delete(scope) unless user
  user
end

#key_for(scope) ⇒ Object



11
12
13
# File 'lib/warden/session_serializer.rb', line 11

def key_for(scope)
  "warden.user.#{scope}.key"
end

#serialize(user) ⇒ Object



15
16
17
# File 'lib/warden/session_serializer.rb', line 15

def serialize(user)
  user
end

#sessionObject

We can’t cache this result because the session can be lazy loaded



49
50
51
# File 'lib/warden/session_serializer.rb', line 49

def session
  env["rack.session"] || {}
end

#store(user, scope) ⇒ Object



23
24
25
26
27
28
# File 'lib/warden/session_serializer.rb', line 23

def store(user, scope)
  return unless user
  method_name = "#{scope}_serialize"
  specialized = respond_to?(method_name)
  session[key_for(scope)] = specialized ? send(method_name, user) : serialize(user)
end

#stored?(scope) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/warden/session_serializer.rb', line 40

def stored?(scope)
  !!session[key_for(scope)]
end