Class: Wee::Application

Inherits:
Object show all
Defined in:
lib/wee/application.rb,
lib/wee/locale.rb

Overview

A Wee::Application manages all Session’s of a single application. It dispatches the request to the correct handler by examining the request.

Defined Under Namespace

Classes: SessionCache

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_sessions = 10_000, &block) ⇒ Application

Creates a new application. The block, when called, must return a new Session instance.

Wee::Application.new { Wee::Session.new(root_component) }


29
30
31
32
33
34
# File 'lib/wee/application.rb', line 29

def initialize(max_sessions=10_000, &block)
  @session_factory = block || raise(ArgumentError)
  @session_ids ||= Wee::IdGenerator::Secure.new
  @sessions = SessionCache.new(max_sessions)
  @mutex = Mutex.new
end

Class Attribute Details

.available_localesObject

Returns the value of attribute available_locales.



15
16
17
# File 'lib/wee/locale.rb', line 15

def available_locales
  @available_locales
end

.default_localeObject

Returns the value of attribute default_locale.



14
15
16
# File 'lib/wee/locale.rb', line 14

def default_locale
  @default_locale
end

.text_domainObject

Returns the value of attribute text_domain.



13
14
15
# File 'lib/wee/locale.rb', line 13

def text_domain
  @text_domain
end

Instance Attribute Details

#available_localesObject



30
31
32
# File 'lib/wee/locale.rb', line 30

def available_locales
  @available_locales || self.class.available_locales
end

#default_localeObject



26
27
28
# File 'lib/wee/locale.rb', line 26

def default_locale
  @default_locale || self.class.default_locale
end

#text_domainObject



22
23
24
# File 'lib/wee/locale.rb', line 22

def text_domain
  @text_domain || self.class.text_domain
end

Class Method Details

.for(component_class, session_class = Wee::Session, *component_args) ⇒ Object



13
14
15
# File 'lib/wee/application.rb', line 13

def self.for(component_class, session_class=Wee::Session, *component_args)
  new { session_class.new(component_class.new(*component_args)) }
end

.load_locale(text_domain, available_locales, default_locale, params = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/wee/locale.rb', line 34

def self.load_locale(text_domain, available_locales, default_locale, params={})
  FastGettext.add_text_domain(text_domain, params)
  @text_domain = text_domain
  @available_locales = available_locales
  @default_locale = default_locale
end

Instance Method Details

#call(env) ⇒ Object

Handles a web request



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/wee/application.rb', line 46

def call(env)
  request = Wee::Request.new(env)

  if request.session_id
    session = @mutex.synchronize { @sessions.fetch(request.session_id) }
    if session and session.alive?
      session.call(env)
    else
      url = request.build_url(:session_id => nil, :page_id => nil)
      Wee::RefreshResponse.new("Invalid or expired session", url).finish
    end
  else
    session = new_session()
    url = request.build_url(:session_id => session.id, :page_id => nil)
    Wee::RedirectResponse.new(url).finish
  end
end

#cleanup_sessionsObject

Garbage collect dead sessions



39
40
41
# File 'lib/wee/application.rb', line 39

def cleanup_sessions
  @mutex.synchronize { @sessions.garbage_collect }
end