views:

384

answers:

2

I have a Rails app that repeatedly talks to another Web server through a wrapper, and I'd like to stick the wrapper in a Singleton class so it's not recreated for every request. Easy enough, I thought:

class AppWrapper < Wrapper
  include Singleton
end
...
wrapper = AppWrapper.instance "url"

Only it doesn't work:

wrong number of arguments (0 for 1)
/usr/lib/ruby/1.8/singleton.rb:94:in `initialize'
/usr/lib/ruby/1.8/singleton.rb:94:in `new'
/usr/lib/ruby/1.8/singleton.rb:94:in `instance'

Wrapper.initialize needs an argument, and apparently it's not getting passed through, since line 94 in question says

@__instance__ = new # look Ma, no argument

How do I work around this? Redefining initialize in AppWrapper doesn't seem to help, and mucking around with Wrapper to separate "set URL" from "initialize" seems suboptimal.

+1  A: 

Are you sure you need a singleton and not a factory . Refer this

NM
The application will never need more than one wrapper, so a Factory seems unnecessary.
jpatokal
A: 

Since you mention something about editing Wrapper as a solution, can't you just use Wrapper directly and do this?

class Wrapper; include Singleton; end

If not, you could use something like this, which will just make sure AppWrapper.new isn't called more than once:

class AppWrapper
  def self.new(*args)
    class << app_wrapper = Wrapper.new(*args)
      include Singleton
    end
    app_wrapper
  end
end

If you need the singleton "Klass.instance" method, you'll have to take either take out the parameter in Wrapper#initialize, or just redefine Singleton#instance to take arguments optionally and passes them to the call to new on line 94.

ehsanul
While my app will never need more than one Wrapper, it's conceivable that a future app might need to connect to multiple Wrappers simultaneously. And yes, I do need the instance method, the whole point is getting access to the Wrapper without recreating the object for every call.
jpatokal