开发者

Sinatra + Rack:Session:Pool

开发者 https://www.devze.com 2022-12-13 19:18 出处:网络
I am using Rack:Session:Pool for memory based sessions. I would like to access the pool instance variables that is contacted in

I am using Rack:Session:Pool for memory based sessions. I would like to access the pool instance variables that is contacted in Rack:Session:Pool so I can see all the active session and contained data. How can I do that from within Sinatra or on the irb prompt.

my initial thought was ::Sinatra:Application::Rack::Session:Pool, but that seems to give me the class and not the curre开发者_开发知识库nt instance so the pool variable is not accessible.


If you are doing this just for development/debugging/poking at code with a stick, you could do some monkey patching and make the pool a global variable.

require 'rubygems'
require 'sinatra'
require 'yaml'

class Rack::Session::Pool
  def initialize app,options={}
    super
    $pool=@pool=Hash.new
    @mutex=Mutex.new
  end
end

use Rack::Session::Pool

get '/' do
  y $pool
  ''
end

Or, you could write a wrapper fn that does that for you.

require 'rubygems'
require 'sinatra'
require 'yaml'

module PoolWrapper
  def self.new *args
    middleware = Rack::Session::Pool.new *args
    $pool=middleware.pool
    middleware
  end
end

use PoolWrapper
# same as above 
#...

For debugging, the monkey patch is probably cleaner as you don't have to change the use call and you can wrap the monkeypatch with something that checks the environment so it is only used during devel,etc,etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消