So I started working on this and it's trickier than I thought. So rather
than go ahead and implement something really complicated only to find out
that it doesn't do what people want, I thought I should try to get some
consensus on functionality first.
The basic problem is that people want to be able to decorate datetime
objects in various ways, for example by turning off validation in
constructors, or caching various calls, or whatever.
Subclassing doesn't work well here, because if someone wants to cache
_and_ ignore validation, it wouldn't be possible, assuming both modules
override new().
So what we really want is to create arbitrary "chains" (class hierarchies)
of the modules.
So assuming our base is DateTime, we might sometimes want a hierarchy like
this:
DT
|
is parent of
|
DT::Cache
|
is parent of
|
DT::UpperCaseAllWords
and maybe sometimes we want:
DT
|
is parent of
|
DT::UpperCaseAllWords
|
is parent of
|
DT::NeverPrintTheNumber9
|
is parent of
|
DT::Mixin::AddAFooMethod
But we don't want to make permanent changes to any class hierarchy, and we
want to be able to create different hierarchies as needed, on the fly.
We also want a nice API. I could do the former fairly easily (I think)
with a somewhat ugly API like:
DT::Wrapper->construct( classes => [ ... ],
constructor => 'new',
params => \%params, );
but that API sucks.
So what I think we really want is this:
my $Wrapper = DT::Wrapper->wrapper( [$class1, $class2, $class3] );
where $class1 is a subclass of $class2, and $class2 is a subclass of
$class3.
If !$class3->isa('DateTime') then we need to add DateTime.pm to the list.
Then we want to be able to do:
my $wrapped_dt = $Wrapper->any_constructor(%params);
print $wrapped_dt->year;
# foo is a new method provided by one of the wrapping classes
print $wrapped_dt->foo;
The trick here is that we A) need to catch all constructor calls and tweak
the return value, because the object returned needs to be wrapped.
Anyway, does this API sound sane? And if it does, anyone have any really
clever implementation ideas? I have some scary ones involved AUTOLOAD and
constructing classes on the fly.
-dave
/*=======================
House Absolute Consulting
www.houseabsolute.com
=======================*/