Dominic Marks
2005-Jul-17 18:44 UTC
Wrapping methods in derivative classes with logic from the super class
Hello, I have a super class called TaskManager, a TaskManager is a separate ruby process which I fork. I have various derivative classes which follow this pattern. For accounting purposes I need to know which of these processes is running and which is not. TaskManager is a decendant of ActiveRecord::Base so I''m keeping track by modifying a field in the table. I can do this in each derivative like this: class FooManager < TaskManager def run self.running = 1 self.save begin # FooManager procedure ensure self.running = 0 self.save end end end What I would like to know is if there is some way I can ''wrap'' the run method in the derivative classes so instead my FooManager class would simply be: class FooManager < TaskManager def run # FooManager procedure end end The begin ... ensure ... end would be defined in the super class. Is there a way I can achieve this? Thanks, -- Dominic Marks
Dominic Marks
2005-Jul-17 21:29 UTC
Re: Wrapping methods in derivative classes with logic from the super class
On Sunday 17 July 2005 19:44, Dominic Marks wrote:> Hello, > > I have a super class called TaskManager, a TaskManager is a separate > ruby process which I fork. I have various derivative classes which > follow this pattern. > > For accounting purposes I need to know which of these processes > is running and which is not. TaskManager is a decendant of > ActiveRecord::Base so I''m keeping track by modifying a field in > the table. > > I can do this in each derivative like this: > > class FooManager < TaskManager > def run > self.running = 1 > self.save > begin > # FooManager procedure > ensure > self.running = 0 > self.save > end > end > end > > What I would like to know is if there is some way I can ''wrap'' the > run method in the derivative classes so instead my FooManager class > would simply be: > > class FooManager < TaskManager > def run > # FooManager procedure > end > end > > The begin ... ensure ... end would be defined in the super class. > > Is there a way I can achieve this?I figured out how I am going to approach this for now. Since I call the run method from my own code, and it will not be called from anywhere else, I can simply write an additional method in TaskManager to do my bidding and then call the run method in the derivative class. So long as none of the decendants interfere with this method, and no code calls the run method directly this meets all my goals. If anyone has a better reccomendation I''d still like to hear it though.> Thanks,Cheers, -- Dominic Marks
Hi, I''d like to write my own method to extend the Time class. My problem is where to put the file once I''m done. Is there any directory where I can put it that will make it automatically available to my whole application, or will I always have to "require" it in each separate file? Thanks. Alison Rowland
On Monday 18 July 2005 01:03, Alison Rowland wrote:> I''d like to write my own method to extend the Time class. My problem > is where to put the file once I''m done. Is there any directory where > I can put it that will make it automatically available to my whole > application, or will I always have to "require" it in each separate > file? Thanks.You can put the file in the lib directory and require it at the end of config/environment.rb Michael -- Michael Schuerig Airtight arguments have mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions
François Beausoleil
2005-Jul-18 13:46 UTC
Re: Wrapping methods in derivative classes with logic from the super class
Hello Dominic ! Dominic Marks said the following on 2005-07-17 17:29:> On Sunday 17 July 2005 19:44, Dominic Marks wrote: > I figured out how I am going to approach this for now. Since I call > the run method from my own code, and it will not be called from anywhere > else, I can simply write an additional method in TaskManager to do > my bidding and then call the run method in the derivative class. So > long as none of the decendants interfere with this method, and no code > calls the run method directly this meets all my goals.What you have described above is the Template Method pattern[1]. If you have never read the Design Patterns book[2], check it out. Bye ! François [1] http://c2.com/cgi/wiki?TemplateMethodPattern [2] http://c2.com/cgi/wiki?DesignPatternsBook