Class: Ooor::SessionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ooor/session_handler.rb

Overview

The SessionHandler allows to retrieve a session with its loaded proxies to OpenERP

Instance Method Summary collapse

Instance Method Details

#connectionsObject



64
# File 'lib/ooor/session_handler.rb', line 64

def connections; @connections ||= {}; end

#get_web_session(key) ⇒ Object



55
56
57
# File 'lib/ooor/session_handler.rb', line 55

def get_web_session(key)
  Ooor.cache.read(key)
end

#noweb_session_spec(config) ⇒ Object



9
10
11
# File 'lib/ooor/session_handler.rb', line 9

def noweb_session_spec(config)
  "#{config[:url]}-#{config[:database]}-#{config[:username]}"
end

#register_session(session) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ooor/session_handler.rb', line 38

def register_session(session)
  if session.config[:session_sharing]
    spec = session.web_session[:session_id]
  elsif session.id != :noweb
    spec = session.id
  else
    spec = noweb_session_spec(session.config)
  end
  set_web_session(spec, session.web_session)
  sessions[spec] = session
end

#reset!Object



50
51
52
53
# File 'lib/ooor/session_handler.rb', line 50

def reset!
  @sessions = {}
  @connections = {}
end

#retrieve_session(config, id = nil, web_session = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ooor/session_handler.rb', line 13

def retrieve_session(config, id=nil, web_session={})
  id ||= SecureRandom.hex(16)
  if id == :noweb
    spec = noweb_session_spec(config)
  else
    spec = id
  end

  s = sessions[spec]
  # reload session or create a new one if no matching session found
  if config[:reload] || !s
    config = Ooor.default_config.merge(config) if Ooor.default_config.is_a? Hash
    Ooor::Session.new(config, web_session, id)

  # found but config mismatch still
  elsif noweb_session_spec(s.config) != noweb_session_spec(config)
    config = Ooor.default_config.merge(config) if Ooor.default_config.is_a? Hash
    Ooor::Session.new(config, web_session, id)

  # matching session, update web_session of it eventually
  else
    s.tap {|s| s.web_session.merge!(web_session)} #TODO merge config also?
  end
end

#sessionsObject



63
# File 'lib/ooor/session_handler.rb', line 63

def sessions; @sessions ||= {}; end

#set_web_session(key, web_session) ⇒ Object



59
60
61
# File 'lib/ooor/session_handler.rb', line 59

def set_web_session(key, web_session)
  Ooor.cache.write(key, web_session)
end