Re: Class::DBI::Plugin
[prev]
[thread]
[next]
[Date index for 2004/07/02]
On Jul 1, 2004, at 2:03 PM, Addison, Mark wrote:
> Yes. It will also need to know the class it is plugging into (and
> maybe some init args)
>
> =A0__PACKAGE__->init_plugin( $class, @args );
>
> How do you use the plugin? ie add it to one of your classes.
You simply "use" it in your database class, which has the effect that=20
all your inheriting table classes now have the new method(s). Or you=20
use it selectively in a table class. That's the way all plugins (like=20
Class::DBI::AbstractSearch, for instance) are used.
> Which would work but could start to get repetative to cover eveything=20=
> the
> plugin may want todo e.g. add TEMP columns, add triggers, add class
> data, etc. You would end up with a constant for each. The init method
> allows the plugin to do whatever it wants to set itself up and as it
> knows the class to plug into it can do stuff to that as well.
>
> e.g. A logging plugin might open a logfile on init and add some=20
> triggers
> to do the logging;
>
> =A0sub init_plugin {
> =A0=A0=A0 my ($class,$plug_class) =3D @_
>
> =A0=A0=A0 my $logfile;
> =A0=A0=A0 open($logfile, ">/var/log/foo.log");
>
> =A0=A0=A0 $plug_class->add_trigger( after_create =3D> sub {
> =A0=A0=A0=A0=A0=A0=A0 print $logfile "[Created] ".ref($[0])." =
id=3D".$[0]->id } );
>
> =A0=A0=A0 $plug_class->add_trigger( after_update =3D> sub {
> =A0=A0=A0=A0=A0=A0=A0 print $logfile "[Updated] ".ref($[0])." =
id=3D".$[0]->id } );
>
> =A0=A0=A0 $plug_class->add_trigger( after_delete =3D> sub {
> =A0=A0=A0=A0=A0=A0=A0 print $logfile "[Deleted] ".ref($[0])." =
id=3D".$[0]->id } );
> =A0}
That is absolutely convincing. I thought about it last night, and the=20
solution was simpler than I expected! There is no need to fetch the=20
plugin class from the parameter list, though. It's a kinda strange way=20=
of thinking: in the plugin class, you "pretend" to be in the class=20
you're plugging in to. So the new interface is:
package Class::DBI::Plugin::AbstractCount;
use base 'Class::DBI::Plugin';
sub init
{
my $class =3D shift;
$class->set_sql( count_search_where =3D> ... );
$class->add_trigger( after_create =3D> ... ); # makes no sense here, =
of=20
course
# and so on...
}
sub count_search_where : Plugged
{
my $class =3D shift;
# ...
$class->sql_count_search_where( ... );
}
Is everyone happy with this?
--=20
J.-C Zeus (mail@xxxxxx.xxx)
|
|
Re: Class::DBI::Plugin
Jean-Christophe Zeus 07:40 on 02 Jul 2004
|