Changeset 2271

Show
Ignore:
Timestamp:
05/29/07 13:02:00
Author:
miyagawa
Message:

merged Jesse's return values branch

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Class-Trigger/trunk/Changes

    r2172 r2271  
    11Revision history for Perl extension Class::Trigger. 
    22 
    3 0.11  Thu Mar  1 17:22:24 PST 2007 
    4         - Make this a release. 
     30.12 
    54 
    6 0.10_01  Thu Feb 22 19:02:09 PST 2007 
     5    - Added support for abortable triggers. 
     6    - Added named-parameters calling convention for add_trigger 
     7    - Added new last_trigger_results method and began capturing trigger return values. 
     8 
     9    - FUNCTIONALITY CHANGE: When no triggers are called for a trigger point, the return value is now '0', not undef. 
     10 
     11      (Thanks to Jesse Vincent) 
     12 
     13 
     140.11  Thu Feb 22 19:02:09 PST 2007 
    715        - Fixed a bug due to Class::Data::Inheritable, where 
    816          triggers added to the superclass after trigger is added in 
  • Class-Trigger/trunk/MANIFEST

    r1618 r2271  
    11Changes 
     2lib/Class/Trigger.pm 
     3Makefile.PL 
    24MANIFEST 
    3 Makefile.PL 
    45README 
    5 lib/Class/Trigger.pm 
    66t/00_compile.t 
    77t/01_trigger.t 
     
    1111t/05_args.t 
    1212t/06_coverage.t 
     13t/07_abortable_callbacks.t 
    1314t/lib/Foo.pm 
    1415t/lib/Foo/Bar.pm 
  • Class-Trigger/trunk/MANIFEST.SKIP

    r400 r2271  
    1010\.gz$ 
    1111\.cvsignore 
     12\.svn 
  • Class-Trigger/trunk/README

    r413 r2271  
    99          my $self = shift; 
    1010          $self->call_trigger('before_foo'); 
    11           $self->do_foo; 
     11          # some code ... 
     12          $self->call_trigger('middle_of_foo'); 
     13          # some code ... 
    1214          $self->call_trigger('after_foo'); 
    1315      } 
     
    1820 
    1921      my $foo = Foo->new; 
    20       $foo->foo;                    # then sub1, sub2 called 
     22      $foo->foo;            # then sub1, sub2 called 
    2123 
    2224      # triggers are inheritable 
     
    2729 
    2830      # triggers can be object based 
    29       $foo->add_hook(after_foo => \&sub3); 
    30       $foo->foo;                    # sub3 would appply only to this object 
     31      $foo->add_trigger(after_foo => \&sub3); 
     32      $foo->foo;            # sub3 would appply only to this object 
    3133 
    3234DESCRIPTION 
     
    4143          $foo->add_trigger($triggerpoint => $sub); 
    4244 
     45          Foo->add_trigger( name => $triggerpoint, 
     46                            callback => sub {return undef}, 
     47                            abortable => 1);  
     48 
     49          # no further triggers will be called. Undef will be returned. 
     50 
    4351        Adds triggers for trigger point. You can have any number of triggers 
    44         for each point. Each coderef will be passed a copy of the object, 
    45         and return values will be ignored
     52        for each point. Each coderef will be passed a reference to the 
     53        calling object, as well as arguments passed in via run_trigger
    4654 
    47         If "add_trigger" is called as object method, whole trigger table 
    48         will be copied onto the object. Then the object should be 
    49         implemented as hash. 
     55        If add_trigger is called with named_parameters and the "abortable" 
     56        parameter is passed a true value, a false return values will stop 
     57        processing of this trigger point. If the trigger is "abortable" 
     58        return the return value from the last callback processed will be 
     59        returned to calling code. 
     60 
     61        If "add_trigger" is called without the "abortable" flag, return 
     62        values will be ignored. 
     63 
     64        If "add_trigger" is called as object method, whole current trigger 
     65        table will be copied onto the object and the new trigger added to 
     66        that. (The object must be implemented as hash.) 
    5067 
    5168          my $foo = Foo->new; 
     
    6077 
    6178    call_trigger 
    62           $foo->call_trigger($triggerpoint); 
     79          $foo->call_trigger($triggerpoint, @args); 
    6380 
    6481        Calls triggers for trigger point, which were added via "add_trigger" 
    65         method. Each triggers will be passed a copy of the object. 
     82        method. Each triggers will be passed a copy of the object as the 
     83        first argument. Remaining arguments passed to "call_trigger" will be 
     84        passed on to each trigger. Triggers are invoked in the same order 
     85        they were defined. 
    6686 
    6787TRIGGER POINTS 
     
    118138        that row, and on DELETE to remove that index row. 
    119139 
    120         See the Class::DBI::mysql::FullTextSearch manpage and its source 
    121         code to see it in action. 
     140        See Class::DBI::mysql::FullTextSearch and its source code to see it 
     141        in action. 
    122142 
    123143AUTHOR 
     
    130150 
    131151SEE ALSO 
    132     the Class::Data::Inheritable manpage 
     152    Class::DBI 
    133153 
  • Class-Trigger/trunk/lib/Class/Trigger.pm

    r2172 r2271  
    33use strict; 
    44use vars qw($VERSION); 
    5 $VERSION = "0.11"; 
     5$VERSION = "0.11_01"; 
    66 
    77use Carp (); 
     
    1717    # export mixin methods 
    1818    no strict 'refs'; 
    19     my @methods = qw(add_trigger call_trigger); 
     19    my @methods = qw(add_trigger call_trigger last_trigger_results); 
    2020    *{"$pkg\::$_"} = \&{$_} for @methods; 
    2121} 
     
    2525 
    2626    my $triggers = __fetch_triggers($proto); 
     27 
     28    if (ref($_[1]) eq 'CODE') {  
     29 
    2730    while (my($when, $code) = splice @_, 0, 2) { 
    2831        __validate_triggerpoint($proto, $when); 
    2932        Carp::croak('add_trigger() needs coderef') unless ref($code) eq 'CODE'; 
    30         push @{$triggers->{$when}}, $code; 
    31     } 
    32  
     33        push @{$triggers->{$when}}, [$code, undef]; 
     34    } 
     35    } 
     36    elsif (grep {'name'} @_) { 
     37        my %args = ( name => undef, callback => undef, abortable => undef, @_); 
     38        my $when= $args{'name'}; 
     39        my $code = $args{'callback'}; 
     40        my $abortable = $args{'abortable'}; 
     41        __validate_triggerpoint($proto, $when); 
     42        Carp::croak('add_trigger() needs coderef') unless ref($code) eq 'CODE'; 
     43        push @{$triggers->{$when}}, [$code, $abortable]; 
     44 
     45 
     46    } else { 
     47        Carp::croak('add_trigger() needs coderef'); 
     48 
     49    } 
    3350    1; 
     51} 
     52 
     53 
     54sub last_trigger_results { 
     55    my $self = shift; 
     56    my $result_store = ref($self) ? $self : ${Class::Trigger::_trigger_results}->{$self}; 
     57    return $result_store->{'_class_trigger_results'}; 
    3458} 
    3559 
     
    3862    my $when = shift; 
    3963 
     64    my @return; 
     65 
     66    my $result_store = ref($self) ? $self : ${Class::Trigger::_trigger_results}->{$self}; 
     67 
     68    $result_store->{'_class_trigger_results'} = []; 
     69 
    4070    if (my @triggers = __fetch_all_triggers($self, $when)) { # any triggers? 
    41         $_->($self, @_) for @triggers; 
     71        for my $trigger (@triggers) { 
     72              my @return = $trigger->[0]->($self, @_); 
     73                push @{$result_store->{'_class_trigger_results'}}, \@return; 
     74                return undef if ($trigger->[1] and not $return[0]); # only abort on false values. 
     75          
     76    } 
    4277    } 
    4378    else { 
     
    4782        __validate_triggerpoint($self, $when); 
    4883    } 
     84 
     85    return scalar @{$result_store->{'_class_trigger_results'}}; 
    4986} 
    5087 
     
    149186=head1 METHODS 
    150187 
    151 By using this module, your class is capable of following two methods. 
     188By using this module, your class is capable of following methods. 
    152189 
    153190=over 4 
     
    158195  $foo->add_trigger($triggerpoint => $sub); 
    159196 
     197 
     198  Foo->add_trigger( name => $triggerpoint, 
     199                    callback => sub {return undef}, 
     200                    abortable => 1);  
     201 
     202  # no further triggers will be called. Undef will be returned. 
     203 
     204 
    160205Adds triggers for trigger point. You can have any number of triggers 
    161 for each point. Each coderef will be passed a the object reference, and 
    162 return values will be ignored. 
     206for each point. Each coderef will be passed a reference to the calling object,  
     207as well as arguments passed in via L<call_trigger>. Return values will be 
     208captured in I<list context>. 
     209 
     210If add_trigger is called with named parameters and the C<abortable> 
     211parameter is passed a true value, a false return value from trigger 
     212code will stop processing of this trigger point and return a C<false> 
     213value to the calling code. 
     214 
     215If C<add_trigger> is called without the C<abortable> flag, return 
     216values will be captured by call_trigger, but failures will be ignored. 
    163217 
    164218If C<add_trigger> is called as object method, whole current trigger 
     
    185239Triggers are invoked in the same order they were defined. 
    186240 
     241If there are no C<abortable> triggers or no C<abortable> trigger point returns  
     242a false value, C<call_trigger> will return the number of triggers processed. 
     243 
     244 
     245If an C<abortable> trigger returns a false value, call trigger will stop execution 
     246of the trigger point and return undef. 
     247 
     248=item last_trigger_results 
     249 
     250    my @results = @{ $foo->last_trigger_results }; 
     251 
     252Returns a reference to an array of the return values of all triggers called 
     253for the last trigger point. Results are ordered in the same order the triggers 
     254were run. 
     255 
     256 
    187257=back 
    188258 
  • Class-Trigger/trunk/t/06_coverage.t

    r1517 r2271  
    66 
    77my $foo = Foo->new(); 
    8 is($foo->call_trigger(), undef, 'no triggers, no action'); 
     8is($foo->call_trigger(), 0, 'no triggers, no action');