tags:

views:

491

answers:

1

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 current instance so the pool variable is not accessible.

+1  A: 

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.

BaroqueBobcat
This doesn't seem to be working. The global variable is 'nil' within in the scope of the sinatra application. Also, I believe the global variable is the above example is being passed by value not reference so when the session a stored in the @pool they will not reflect in the $pool
Peter
It is passed by value, but the value is a pointer to the same hash object. It shouldn't be `nil` unless it was not assigned.
BaroqueBobcat
Well I tested the "monkey patch" version again and it worked!. I believe the issue was that call "y $pool" returned and empty string. I just called "$pool.to_yaml" and it worked.
Peter
Oh, sorry. I like using `y`, it is like `p` only it outputs the result of `to_yaml` instead of `inspect` which I think is more readable.
BaroqueBobcat