Class: TrickSerial::Serializer::CgiSession::Store
- Inherits:
-
Object
- Object
- TrickSerial::Serializer::CgiSession::Store
- 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
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#logger_level ⇒ Object
Returns the value of attribute logger_level.
Instance Method Summary collapse
- #_dbman ⇒ Object
- #_log(msg = nil) ⇒ Object
- #_make_serializer ⇒ Object
- #close ⇒ Object
- #delete ⇒ Object
-
#initialize(session, option = {}) ⇒ Store
constructor
Options:.
- #restore ⇒ Object
- #update ⇒ Object
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
#logger ⇒ Object
Returns the value of attribute logger.
47 48 49 |
# File 'lib/trick_serial/serializer/cgi_session.rb', line 47 def logger @logger end |
#logger_level ⇒ Object
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
#_dbman ⇒ Object
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_serializer ⇒ Object
86 87 88 |
# File 'lib/trick_serial/serializer/cgi_session.rb', line 86 def _make_serializer (@serializer || TrickSerial::Serializer.default).dup end |
#close ⇒ Object
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 |
#delete ⇒ Object
120 121 122 123 |
# File 'lib/trick_serial/serializer/cgi_session.rb', line 120 def delete _log { "#{self} delete" } _dbman.delete end |
#restore ⇒ Object
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 |
#update ⇒ Object
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 |