Class: Innate::Session

Inherits:
Object
  • Object
show all
Includes:
Optioned
Defined in:
lib/innate/session.rb,
lib/innate/session/flash.rb

Overview

Mostly ported from Ramaze, but behaves lazy, no session will be created if no session is used.

We keep session data in memory until #flush is called, at which point it will be persisted completely into the cache, no question asked.

You may store anything in here that you may also store in the corresponding store, usually it’s best to keep it to things that are safe to Marshal.

The default time of expiration is *

Time.at(2147483647) # => Tue Jan 19 12:14:07 +0900 2038

Hopefully we all have 64bit systems by then.

Defined Under Namespace

Classes: Flash

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Optioned

included

Constructor Details

#initialize(request, response) ⇒ Session

Returns a new instance of Session.



36
37
38
39
40
41
# File 'lib/innate/session.rb', line 36

def initialize(request, response)
  @request, @response = request, response
  @cookie_set = false
  @cache_sid = nil
  @flash = Flash.new(self)
end

Instance Attribute Details

Returns the value of attribute cookie_set.



34
35
36
# File 'lib/innate/session.rb', line 34

def cookie_set
  @cookie_set
end

#flashObject (readonly)

Returns the value of attribute flash.



34
35
36
# File 'lib/innate/session.rb', line 34

def flash
  @flash
end

#requestObject (readonly)

Returns the value of attribute request.



34
35
36
# File 'lib/innate/session.rb', line 34

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



34
35
36
# File 'lib/innate/session.rb', line 34

def response
  @response
end

Instance Method Details

#[](key) ⇒ Object



47
48
49
# File 'lib/innate/session.rb', line 47

def [](key)
  cache_sid[key]
end

#[]=(key, value) ⇒ Object



43
44
45
# File 'lib/innate/session.rb', line 43

def []=(key, value)
  cache_sid[key] = value
end

#cache_sidObject



60
61
62
# File 'lib/innate/session.rb', line 60

def cache_sid
  @cache_sid ||= cache[sid] || {}
end

#clearObject



55
56
57
58
# File 'lib/innate/session.rb', line 55

def clear
  cache.delete(sid)
  @cache_sid = nil
end


77
78
79
# File 'lib/innate/session.rb', line 77

def cookie
  @request.cookies[options.key]
end

#delete(key) ⇒ Object



51
52
53
# File 'lib/innate/session.rb', line 51

def delete(key)
  cache_sid.delete(key)
end

#flush(response = @response) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/innate/session.rb', line 64

def flush(response = @response)
  return if !@cache_sid or @cache_sid.empty?

  flash.rotate!
  ttl = (Time.at(cookie_value[:expires]) - Time.now).to_i
  cache.store(sid, cache_sid, :ttl => ttl)
  set_cookie(response)
end

#inspectObject



81
82
83
# File 'lib/innate/session.rb', line 81

def inspect
  cache.inspect
end

#sidObject



73
74
75
# File 'lib/innate/session.rb', line 73

def sid
  @sid ||= cookie || generate_sid
end