Class: TrickSerial::Serializer::CgiSession::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/trick_serial/serializer/cgi_session.rb

Overview

Defines a Session store Decorator that interjects TrickSerial::Serializer inside #restore, #update, etc.

Example:

cgi = CGI.new("html4")
session = CGI::Session.new(
  'database_manager' => TrickSerial::Serializer::CgiSession::Store, # The Decorator.
  'TrickSerial.database_manager' => CGI::Session::PStore, # Actual store Class.
  # Options for PStore instance:
  'tmpdir' => '/tmp/mysessions',
  'session_key' => 'mykey',
  ...
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, option = {}) ⇒ Store

Options:

'TrickSerial.database_manager': the actual session Store class (e.g.: CGI::Session::PStore).
'TrickSerial.dbman': an actual session store instance.
'TrickSerial.serializer': a clonable instance of TrickSerial::Serializer.
'TrickSerial.logger': a Log4r object.
'TrickSerial.logger_level': a Symbol for the logger level (e.g: :debug)

The remaining options are passed to the actual Store specified by :‘TrickSerial.database_manager’.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/trick_serial/serializer/cgi_session.rb', line 59

def initialize(session, option={})
  @session = session
  @option = option
  @dbman_cls = option.delete('TrickSerial.database_manager') ||
    (raise "#{self} options did not specify TrickSerial.database_manager: #{option.inspect}")
  @dbman = option.delete('TrickSerial.dbman')
  # option['new_session'] = true 
  @option['database_manager'] = @dbman_cls
  @serializer = option.delete('TrickSerial.serializer')
  @logger = option.delete('TrickSerial.logger')
  @logger_level = option.delete('TrickSerial.logger_level') || :debug
  _log { "creating #{self} for #{option.inspect}" }
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



47
48
49
# File 'lib/trick_serial/serializer/cgi_session.rb', line 47

def logger
  @logger
end

#logger_levelObject

Returns the value of attribute logger_level.



47
48
49
# File 'lib/trick_serial/serializer/cgi_session.rb', line 47

def logger_level
  @logger_level
end

Instance Method Details

#_dbmanObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/trick_serial/serializer/cgi_session.rb', line 73

def _dbman
  @dbman ||= 
    begin
      # Fool decorated Store.
      save = @session.new_session
      @session.new_session = true
      # debugger
      @dbman_cls.new(@session, @option)
    ensure
      @session.new_session = save
    end
end

#_log(msg = nil) ⇒ Object



125
126
127
128
129
130
# File 'lib/trick_serial/serializer/cgi_session.rb', line 125

def _log msg = nil
  msg ||= yield if block_given?
  if msg && @logger
    @logger.send(@logger_level, msg)
  end
end

#_make_serializerObject



86
87
88
# File 'lib/trick_serial/serializer/cgi_session.rb', line 86

def _make_serializer
  (@serializer || TrickSerial::Serializer.default).dup
end

#closeObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/trick_serial/serializer/cgi_session.rb', line 109

def close
  _log { "#{self} close" }
  serializer = _make_serializer
  data_save = _dbman._data
  _dbman._data = serializer.encode(_dbman._data)
  _dbman.encode_with_trick_serial_serializer! if _dbman.respond_to?(:encode_with_trick_serial_serializer!)
  _dbman.close
ensure
  _dbman._data = data_save
end

#deleteObject



120
121
122
123
# File 'lib/trick_serial/serializer/cgi_session.rb', line 120

def delete
  _log { "#{self} delete" }
  _dbman.delete
end

#restoreObject



90
91
92
93
94
95
# File 'lib/trick_serial/serializer/cgi_session.rb', line 90

def restore
  _log { "#{self} restore" }
  _dbman.restore
  _dbman.decode_with_trick_serial_serializer! if _dbman.respond_to?(:decode_with_trick_serial_serializer!)
  _dbman._data
end

#updateObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/trick_serial/serializer/cgi_session.rb', line 97

def update
  _log { "#{self} update" }
  serializer = _make_serializer
  data_save = _dbman._data
  _dbman._data = serializer.encode(_dbman._data)
  _dbman.encode_with_trick_serial_serializer! if _dbman.respond_to?(:encode_with_trick_serial_serializer!)
  # debugger
  _dbman.update
ensure
  _dbman._data = data_save
end