Hello I am trying to spec a singleton ruby class, And I am having a little trouble because my specs are not more isolated, as I am getting the same instace of the class for all my examples. How should I do this.? Which is the correct way of specing a singleton class? Thanks Juanma -- Posted via http://www.ruby-forum.com/.
On Tue, Jan 19, 2010 at 6:03 AM, Juanma Cervera <lists at ruby-forum.com> wrote:> Hello > I am trying to spec a singleton ruby class, > And I am having a little trouble because my specs are not more isolated, > as I am getting the same instace of the class for all my examples. > > How should I do this.? > Which is the correct way of specing a singleton class?I''m assuming you mean singleton as in the Singleton Pattern, as opposed to the singleton class of an object, yes? If so, you''ll have to dup it or subclass it in your spec if you want to isolation. Why do you need a Singleton, though? David> > Thanks > Juanma > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Yes, I mean the Singleton Pattern. I am not an expert with OO, but I supposed this pattern was what best fits my needs of a class that represent a unique resource in the system, in this case it''s something like a queue of jobs that I have to administrate in real time. Am I right? Maybe not, because I am writing a web app and not using globals or session state to store this class, and I am accessing the instance of the class in each http request. In that case the dup option sounds good for me. But what do you think? Thanks David Juanma David Chelimsky wrote:> On Tue, Jan 19, 2010 at 6:03 AM, Juanma Cervera <lists at ruby-forum.com> > wrote: >> Hello >> I am trying to spec a singleton ruby class, >> And I am having a little trouble because my specs are not more isolated, >> as I am getting the same instace of the class for all my examples. >> >> How should I do this.? >> Which is the correct way of specing a singleton class? > > I''m assuming you mean singleton as in the Singleton Pattern, as > opposed to the singleton class of an object, yes? > > If so, you''ll have to dup it or subclass it in your spec if you want > to isolation. > > Why do you need a Singleton, though? > > David-- Posted via http://www.ruby-forum.com/.
On Jan 19, 2010, at 2:33 pm, Juanma Cervera wrote:> Yes, I mean the Singleton Pattern. > I am not an expert with OO, but I supposed this pattern was what best > fits my needs of a class that represent a unique resource in the system, > in this case it''s something like a queue of jobs that I have to > administrate in real time. > Am I right?What would be the implications of *not* using a singleton? Bear in mind that if you''re writing a webapp, you will at some point have two of these objects simultaneously in different processes. What is the responsibility of the object itself, and where does the responsibility for enforcing that the resource is only used by one client at a time live? Ashley -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashleymoran