Re: PATCH: add_trigger()
[prev]
[thread]
[next]
[Date index for 2004/10/01]
On Thu, 30 Sep 2004 18:21:11 -0400, Edward J. Sabol
<sabol@xxxxxxxx.xxxx.xxxx.xxx> wrote:
> Tony Bowden wrote:
> >> I'm not sure this plays out if you want to add other trigger points
> >> of your own, though.
>
> Drew Taylor replied:
> > We could always have a mutator to add to the standard trigger points.
> > It might be a good thing to have your application level triggers
> > documented somewhere. :-)
>
> You mean something like
>
> __PACKAGE__->add_valid_trigger('my_trigger');
> __PACKAGE__->add_trigger('my_trigger', ...);
>
> ? That could work. As long as we retain the capability to add our own
> application-level triggers, it sounds like a good idea to me.
I like the method name, so here's an updated patch. Tony, are you
interested in adding this to CDBI?
--- Class-DBI.orig.pm Thu Sep 30 15:55:17 2004
+++ Class-DBI.pm Fri Oct 1 09:49:40 2004
@@ -104,6 +104,13 @@
__PACKAGE__->mk_classdata('__driver');
__PACKAGE__->__data_type({});
+__PACKAGE__->mk_classdata('__valid_triggers');
+__PACKAGE__->__valid_triggers({ map{$_=>undef} qw(before_create after_create
+ before_update after_update
+ before_delete after_delete
+ select create)
+ });
+
__PACKAGE__->mk_classdata('iterator_class');
__PACKAGE__->iterator_class('Class::DBI::Iterator');
__PACKAGE__->__grouper(Class::DBI::ColumnGrouper->new());
@@ -1000,9 +1007,34 @@
$self->_carp(
"$name trigger deprecated: use before_$name or
after_$name instead")
if ($name eq "create" or $name eq "delete");
+ return $self->_croak("Invalid trigger [$name]")
+ unless $self->__is_valid_trigger($name);
+
$self->SUPER::add_trigger($name => @args);
}
+sub add_valid_trigger {
+ my ($class, @names) = shift;
+ return $class->_croak("You must specify a trigger name")
+ unless $name;
+
+ my $valid = $class->__valid_triggers;
+ foreach (@names) { $valid->{$_} = undef; }
+
+ $class->__valid_triggers($valid);
+}
+
+sub __is_valid_trigger {
+ my ($self, $name) = @_;
+ my %valid = %{ $self->__valid_triggers };
+ foreach my $col ($self->all_columns) {
+ $valid{$col} = undef;
+ }
+
+ return 0 unless exists $valid{$name};
+ return 1;
+}
+
#----------------------------------------------------------------------
# Inflation
#----------------------------------------------------------------------
@@ -1785,7 +1817,14 @@
You can create any number of triggers for each point, but you cannot
specify the order in which they will be run. Each will be passed the
object being dealt with (whose values you may change if required),
-and return values will be ignored.
+and return values will be ignored. If you specify an invalid trigger
+in the call to add_trigger() an error will be thrown.
+
+If you want to add other trigger points of your own, such as application
+level triggers, you can use add_valid_trigger() to add these to the
+list of valid triggers.
+
+ __PACKAGE__->add_valid_trigger('my_trigger');
All triggers are passed the object they are being fired for.
Some triggers are also passed extra parameters as name-value pairs.
--
----------------------------------------------------------------
Drew Taylor * Web development & consulting
Email: drew@xxxxxxxxxx.xxx * Site implementation & hosting
Web : www.drewtaylor.com * perl/mod_perl/DBI/mysql/postgres
----------------------------------------------------------------
|
(message missing)
|