From: Aleks Kissinger Date: 2006-07-26T22:30:11+09:00 Subject: Re: Automagic class prototyping I can see some pitfalls here, but this is the most promising technique I've seen so far. On 7/26/06, ara.t.howard@noaa.gov wrote: > On Wed, 26 Jul 2006, Aleks Kissinger wrote: > > > Anybody know any slick techniques for automatically prototyping > > classes so they can interact with each other in top-level code? I've > > run through two methods, but I'm not real wild about either of them. > > > > Say for example, classes represent some security label and I want to > > allow things with one label to access those of another label. So, > > these hypothetical classes all descent from Sec.... > > > > class Sec > > class < > def allow(c) > > (@allowed_classes||=[]) < > end > > end > > end > > > > > > And I want to ultimately just indescriminately run stuff like this: > > > > class Documents < Sec > > allow Downloads > > end > > > > class Downloads < Sec > > allow Documents > > end > > > > But this is a run-time error unless the classes are prototyped somehow. > > Like: > > > > class Downloads < Sec; end > > > > class Documents < Sec > > allow Downloads > > end > > > > class Downloads < Sec > > allow Documents > > end > > > > As a framework, this would be so much nicer for the end-user if it > > does that automatically, so she doesnt have to thing about declaration > > order. So thats the problem. Possible solutions I've tried: > > > harp:~ > cat a.rb > class Sec > ALLOWED_CLASSES = [] > > def self.allowed_classes > ALLOWED_CLASSES > end > > def self.allow *list > allowed_classes.push *list > allowed_classes.uniq! > allowed_classes > end > > def self.const_missing c > c > end > > def self.allowed > allowed_classes.map{|c| eval c.to_s } > end > end > > class Documents < Sec > allow Downloads > end > > class Foobar < Sec > allow Downloads > end > > class Downloads < Sec > allow Documents, Foobar > end > > p Sec.allowed > > > > harp:~ > ruby a.rb > [Downloads, Documents, Foobar] > > > i'd push/pop that 'const_missing' handler during the 'allow' method to avoid > any nasty recursion. but there ya go. > > -a > -- > suffering increases your inner strength. also, the wishing for suffering > makes the suffering disappear. > - h.h. the 14th dali lama > >