Class: Innate::Session

Inherits:
Object
  • Object
show all
Includes:
Optioned
Defined in:
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb,
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/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 Session instance is compatible with the specification of rack.session.

Since the Time class is used to create the cookie expiration timestamp, you will have to keep the ttl in a reasonable range. The maximum value that Time can store on a 32bit system is: Time.at(2147483647) # => Tue Jan 19 12:14:07 +0900 2038

The default expiration time for cookies and the session cache was reduced to a default of 30 days. This was done to be compatible with the maximum ttl of MemCache. You may increase this value if you do not use MemCache to persist your sessions.

Defined Under Namespace

Classes: Flash

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Optioned

included, #options

Constructor Details

- (Session) initialize(request, response)

Returns a new instance of Session



47
48
49
50
51
52
53
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 47

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

Instance Attribute Details

Returns the value of attribute cookie_set



45
46
47
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 45

def cookie_set
  @cookie_set
end

- (Object) flash (readonly)

Returns the value of attribute flash



45
46
47
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 45

def flash
  @flash
end

- (Object) request (readonly)

Returns the value of attribute request



45
46
47
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 45

def request
  @request
end

- (Object) response (readonly)

Returns the value of attribute response



45
46
47
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 45

def response
  @response
end

Instance Method Details

- (Object) cache (private)



107
108
109
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 107

def cache
  Innate::Cache.session
end

- (Object) cache_sid (private)



99
100
101
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 99

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

- (Object) clear



71
72
73
74
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 71

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


103
104
105
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 103

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


119
120
121
122
123
124
125
126
127
128
129
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 119

def cookie_value
  o = options
  cookie = {
    :domain => o.domain,
    :path => o.path,
    :secure => o.secure,
    :httponly => o.httponly
  }
  cookie[:expires] = (Time.now + o.ttl) if o.ttl
  cookie.merge!(:value => sid)
end

- (Object) delete(key)



67
68
69
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 67

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

- (Object) fetch(key, value = nil) Also known as: []



62
63
64
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 62

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

- (Object) flush(response = @response)

Additional interface



78
79
80
81
82
83
84
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 78

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

  flash.rotate!
  cache.store(sid, cache_sid, :ttl => options.ttl)
  set_cookie(response)
end

- (Object) generate_sid (private)



131
132
133
134
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 131

def generate_sid
  begin sid = sid_algorithm end while cache[sid]
  sid
end

- (Object) resid!



90
91
92
93
94
95
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 90

def resid!
  cache_sid
  cache.delete(sid)
  @sid = generate_sid
  @force_new_cookie = true
end


111
112
113
114
115
116
117
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 111

def set_cookie(response)
  return if @cookie_set || (!@force_new_cookie && cookie)

  @cookie_set = true
  response.set_cookie(options.key, cookie_value)
  @force_new_cookie = false
end

- (Object) sid



86
87
88
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 86

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

- (Object) store(key, value) Also known as: []=

Rack interface



57
58
59
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/session.rb', line 57

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