Class: Wee::Session

Inherits:
Object show all
Includes:
LRUCache::Item
Defined in:
lib/wee/locale.rb,
lib/wee/session.rb

Defined Under Namespace

Classes: AbortProcessing, MutexSerializer, Page, ThreadSerializer

Instance Attribute Summary collapse

Attributes included from LRUCache::Item

#lru_time

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_component, serializer = nil, page_cache_capacity = 20) ⇒ Session

Creates a new session.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/wee/session.rb', line 103

def initialize(root_component, serializer=nil, page_cache_capacity=20)
  @root_component = root_component
  @page_cache = Wee::LRUCache.new(page_cache_capacity)
  @page_ids = Wee::IdGenerator::Sequential.new
  @current_page = nil

  @running = true

  @expire_after = 30*60
  @max_lifetime = nil
  @max_requests = nil

  @last_access = @creation_time = Time.now 
  @request_count = 0
  
  @serializer = serializer || MutexSerializer.new
end

Instance Attribute Details

#applicationObject

Points to the Wee::Application object this session belongs to.



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

def application
  @application
end

#expire_afterObject

Expire the session after this number of seconds of inactivity. If this value is nil, the Session will never expire due to inactivity. (but still may expire for example due to max_lifetime).

Default: 1800 seconds (30 minutes)



82
83
84
# File 'lib/wee/session.rb', line 82

def expire_after
  @expire_after
end

#idObject

The (application-wide) unique id of this session.



68
69
70
# File 'lib/wee/session.rb', line 68

def id
  @id
end

#localeObject



45
46
47
# File 'lib/wee/locale.rb', line 45

def locale
  @locale || application.default_locale
end

#max_lifetimeObject

The maximum lifetime of this session in seconds. A value of nil means infinite lifetime.

Default: nil (infinite lifetime)



90
91
92
# File 'lib/wee/session.rb', line 90

def max_lifetime
  @max_lifetime
end

#max_requestsObject

The maximum number of requests this session is allowed to serve. A value of nil means no limitation.

Default: nil (infinite number of requests)



98
99
100
# File 'lib/wee/session.rb', line 98

def max_requests
  @max_requests
end

Class Method Details

.currentObject

Returns the current session (thread-local).



167
168
169
# File 'lib/wee/session.rb', line 167

def self.current
  Thread.current[:wee_session] || (raise "Not in session")
end

Instance Method Details

#alive?Boolean

Queries whether the session is still alive.

Returns:

  • (Boolean)


134
135
136
137
138
139
140
141
# File 'lib/wee/session.rb', line 134

def alive?
  now = Time.now
  return false if not @running
  return false if @expire_after and now - @last_access > @expire_after 
  return false if @max_lifetime and now - @creation_time > @max_lifetime 
  return false if @max_requests and @request_count >= @max_requests
  return true
end

#call(env) ⇒ Object

Handles a web request.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/wee/session.rb', line 174

def call(env)
  if env['wee.session']
    # we are already serialized
    raise if env['wee.session'] != self
    begin
      Thread.current[:wee_session] = self
      @request_count += 1
      @last_access = Time.now
      awake
      response = handle(env)
      sleep
      return response
    ensure
      Thread.current[:wee_session] = nil
    end
  else
    env['wee.session'] = self
    @serializer.call(env)
  end
end

#dead?Boolean

Queries whether the session is dead.

Returns:

  • (Boolean)


146
147
148
# File 'lib/wee/session.rb', line 146

def dead?
  not alive?
end

#render_ajax_proc(block, component) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/wee/session.rb', line 260

def render_ajax_proc(block, component)
  proc {
    r = component.renderer_class.new
    r.session   = self
    r.request   = @request
    r.response  = Wee::Response.new
    r.document  = Wee::HtmlDocument.new
    r.callbacks = @page.callbacks
    r.current_component = component

    begin
      block.call(r)
    ensure
      r.close
    end

    r.response << r.document.to_s
    send_response(r.response)
  }
end

#send_response(response) ⇒ Object

Send a premature response

Raises:



198
199
200
# File 'lib/wee/session.rb', line 198

def send_response(response)
  raise AbortProcessing.new(response)
end

#statisticsObject

Returns some statistics



153
154
155
156
157
158
159
160
161
162
# File 'lib/wee/session.rb', line 153

def statistics
  now = Time.now
  {
    :last_access => @last_access,        # The time when this session was last accessed
    :inactivity => now - @last_access,   # The number of seconds of inactivity
    :creation_time => @creation_time,    # The time at which this session was created 
    :lifetime =>  now - @creation_time,  # The lifetime of this session in seconds
    :request_count => @request_count     # The number of requests served by this session
  }
end

#terminateObject

Terminates the session.

This will usually not immediatly terminate the session from running, but further requests will not be answered.



127
128
129
# File 'lib/wee/session.rb', line 127

def terminate
  @running = false
end