From: ahoward Date: 2002-11-09T01:53:22+09:00 Subject: Re: CGI::Session::FileStore question On Thu, 7 Nov 2002, Yukihiro Matsumoto wrote: [snip] > It is not too hard to define something like CGI::Session::PStore. > Sufficient for your homework. i think this is not _directly_ possible because of []= in CGI::Session we have def []=(key, val) unless @write_lock @write_lock = true end unless @data @data = @dbman.restore end @data[key] = String(val) # <--- problem here? end seems like this should only be @data[key] = val and left to the individual dbman to stringify, or not. i did create a working PStoreStore by making this change (baby unit test included) but i thought one should not have to change CGI::Session require 'cgi/session' require 'pstore' class CGI class Session def []=(key, val) unless @write_lock @write_lock = true end unless @data @data = @dbman.restore end #@data[key] = String(val) @data[key] = val end class PStoreStore def check_id(id) /[^0-9a-zA-Z]/ =~ id.to_s ? false : true end def initialize session, option={} dir = option['tmpdir'] || ENV['TMP'] || '/tmp' prefix = option['prefix'] || '' id = session.session_id unless check_id(id) raise ArgumentError, "session_id `%s' is invalid" % id end path = dir+"/"+prefix+id path.untaint unless File::exist? path @hash = {} end @p = PStore.new path end def restore unless @hash @p.transaction do begin @hash = @p['hash'] rescue @hash = {} end end end @hash end def update @p.transaction do @p['hash'] = @hash end end def close update end def delete path = @p.path File::unlink path end end end end if $0 == __FILE__ require 'cgi' $stdin = open '|echo' cgi = CGI.new $stdin = STDIN session = CGI::Session.new cgi, 'database_manager' => CGI::Session::PStoreStore session['key'] = {'k' => 'v'} puts session['key'].type fail unless Hash === session['key'] puts session['key'].inspect fail unless session['key'].inspect == '{"k"=>"v"}' end -ara -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ahoward@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================