Inheritance question.
[prev]
[thread]
[next]
[Date index for 2004/06/15]
Hello, all. I've got a question about CDBI inheritance. Here's a much-simplified example that I hope conveys the problem:
===
package Music::CD::BASE;
use base (CDBI);
__PACKAGE__->columns(Primary => qw(id));
__PACKAGE__->columns(Essential => qw(artist producer distributor));
sub artist { arbitrary_modification($_[0]->_artist_accessor) }
===
package Music::CD;
use base qw(Music::CD::BASE);
__PACKAGE__->columns(Essential => grep($_ ne 'id', __PACKAGE__->columns('Essential')), othercol1, othercol2, othercol3);
sub foo { }
===
package Music::CD::Cacheable;
use base qw(CDBI::CACHEABLE Music::CD::BASE);
sub bar { }
===
(It's probably not germane, but CDBI is your normal DB setup class, and CDBI::CACHABLE is simply a package that provides an alternate retrieve() method.)
The idea is that Music::CD and Music::CD::Cacheable both want to inherit the methods of Music::CD::BASE.
Being more robust than Music::CD::Cacheable, Music::CD wants to add to the inherited methods a few accessors of its own; to do this, it's got to call columns(). The problem is, there's no public interface (that I know of) to *add* columns to the Essential group; you can only replace the entire group. So it's then necessary to explicitly list the Essential columns already given to Music::CD::Base (the ones we want to inherit, minus the primary column). That's fine as hacks go, but in so doing, we've created the accessors "artist," "producer," and "distributor" in the Music::CD namespace, which hides the special "artist" method we had added to Music::CD::BASE to return an arbitrarily tweaked "artist" value.
Apparently, one could directly manipulate the "Essential" group via __grouper() in each class, but the double underscores are intimidating.
My solution currently is to delete "artist" from the symbol table after the columns() call in Music::CD, but that's obviously pretty gross.
Would it be a bad idea to have a class method add_columns() in addition to columns() to support this type of inheritance? Or is there a completely different way to set this sort of thing up? Or is this type of inheritance a Bad Thing for reasons I don't grasp?
Anyway, thanks for any advice, and thanks for the incredible module.
TRL
|
Inheritance question.
trlorenz 05:55 on 15 Jun 2004
|