Class: Session

Inherits:
Object
  • Object
show all
Defined in:
lib/railz_lite/controllers/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(req) ⇒ Session

find the cookie for this app deserialize the cookie into a hash



6
7
8
9
# File 'lib/railz_lite/controllers/session.rb', line 6

def initialize(req)
  cookie = req.cookies['_rails_lite_app']
  @data = !cookie.nil? ? JSON.parse(cookie) : {}
end

Instance Method Details

#[](key) ⇒ Object



11
12
13
# File 'lib/railz_lite/controllers/session.rb', line 11

def [](key)
  @data[key]
end

#[]=(key, val) ⇒ Object



15
16
17
# File 'lib/railz_lite/controllers/session.rb', line 15

def []=(key, val)
  @data[key] = val
end

#store_session(res) ⇒ Object

serialize the hash into json and save in a cookie add to the responses cookies



21
22
23
24
25
26
27
28
# File 'lib/railz_lite/controllers/session.rb', line 21

def store_session(res)
  res.set_cookie(
    '_rails_lite_app',
    { path: '/',
      value: @data.to_json
    }
  )
end