Show
Ignore:
Timestamp:
12/20/07 14:12:50 (13 months ago)
Author:
nyarla
Message:

lang/perl/Class-Hookable: I changed the target the delete_plugin method deletes.

Location:
lang/perl/Class-Hookable/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Class-Hookable/trunk/lib/Class/Hookable.pm

    r3205 r3345  
    202202        for my $method ( keys %{ $self->hookable_all_methods } ) { 
    203203            my $plugin  = $self->hookable_all_methods->{$method}->{'plugin'}; 
     204            next if ( ! defined $plugin ); 
    204205            my $class   = ref $plugin || $plugin; 
    205206            if ( $is_class ) { 
     
    240241    } 
    241242    else { 
    242         for my $plugin ( @plugins ) { 
    243             $self->delete_plugin( $plugin, $hook ); 
    244         } 
    245     } 
     243        my @new; 
     244        for my $action ( $self->registered_callbacks( $hook ) ) { 
     245            my $plugin = $action->{'plugin'}; 
     246            my $class  = ref $plugin || $plugin; 
     247            for my $object ( @plugins ) { 
     248                if ( ref $object && ! Scalar::Util::blessed($object) ) { 
     249                    Carp::croak "Argument is not blessed object or class name."; 
     250                } 
     251 
     252                my $is_class = ( ! ref $object ) ? 1 : 0 ; 
     253                if ( $is_class ) { 
     254                    push @new, $action if ( $class ne $object ); 
     255                } 
     256                else { 
     257                    push @new, $action if ( $plugin ne $object ); 
     258                } 
     259            } 
     260        } 
     261        $self->hookable_all_hooks->{$hook} = \@new; 
     262    }  
    246263} 
    247264 
     
    267284 
    268285sub delete_method { 
    269     my ( $self, $method ) = @_; 
     286    my ( $self, $method, @plugins ) = @_; 
    270287 
    271288    Carp::croak "Method name is not specified." 
    272289        if ( ! defined $method ); 
    273290 
    274     delete $self->hookable_all_methods->{$method}; 
     291    return if ( ! defined $self->hookable_all_methods->{$method} ); 
     292 
     293    my $plugin = $self->hookable_all_methods->{$method}->{'plugin'}; 
     294    my $class  = ref $plugin || $plugin; 
     295 
     296    if ( @plugins == 0 ) { 
     297        delete $self->hookable_all_methods->{$method}; 
     298    } 
     299    else { 
     300        for my $object ( @plugins ) { 
     301            my $is_class = ( ! ref $object ) ? 1 : 0 ; 
     302            if ( $is_class ) { 
     303                delete $self->hookable_all_methods->{$method} 
     304                    if ( $class eq $object ); 
     305            } 
     306            else { 
     307                delete $self->hookable_all_methods->{$method} 
     308                    if ( $plugin eq $object ); 
     309            } 
     310        } 
     311    } 
    275312} 
    276313 
     
    293330 
    294331sub delete_plugin { 
    295     my ( $self, $object, @hooks ) = @_; 
     332    my ( $self, $object, @points ) = @_; 
    296333 
    297334    if ( ref $object && ! Scalar::Util::blessed($object) ) { 
     
    299336    } 
    300337 
    301     my $is_class = ( ! ref $object ) ? 1  : 0 ; 
    302     @hooks = keys %{ $self->hookable_all_hooks } if ( @hooks == 0 ); 
    303  
    304     for my $hook ( $self->registered_hooks( $object ) ) { 
    305         next if ( ! grep { $hook eq $_ } @hooks ); 
    306  
    307         my @actions = (); 
    308         for my $action ( $self->registered_callbacks( $hook ) ) { 
    309             my $plugin  = $action->{'plugin'}; 
    310             my $class   = ref $plugin || $plugin; 
    311             if ( $is_class ) { 
    312                 push @actions, $action if ( $class ne $object ); 
    313             } 
    314             else { 
    315                 push @actions, $action if ( $plugin ne $object ); 
    316             } 
    317         } 
    318  
    319         $self->hookable_all_hooks->{$hook} = \@actions; 
    320     } 
    321  
     338    if ( @points == 0 ) { 
     339        push @points, $self->registered_hooks( $object ); 
     340        push @points, $self->registered_methods( $object ); 
     341    } 
     342 
     343    for my $point ( @points ) { 
     344        $self->delete_hook( $point => $object ); 
     345        $self->delete_method( $point => $object ); 
     346    } 
    322347} 
    323348 
     
    787812 
    788813  $hook->delete_plugin( $plugin ); 
    789   $hook->delete_plugin( ClassName => qw( hook.A hook.B ) ); 
     814  $hook->delete_plugin( ClassName => qw( hook.A method.A ) ); 
    790815 
    791816This method deletes a registered plugin. 
    792817 
    793818A plugin object or class name is specified as the first argument, 
    794 and some hook names is specified as an argument after that. 
     819and hook names or method names are specified as an argument after that. 
    795820 
    796821When specifying only a plugin object (or class name) as an argument, 
    797 a plugin is deleted from all hooks. 
    798  
    799 And when specifying a plugin object (or class name) and hooks as arguments, 
    800 a plugin is deleted from specified hooks. 
     822a plugin is deleted from all hooks and all methods. 
     823 
     824And when specifying a plugin object (or class name) and hook names or method names as arguments, 
     825a plugin is deleted from specified hooks and specified methods. 
    801826 
    802827=head1 ACCESSOR METOHDS 
  • lang/perl/Class-Hookable/trunk/t/03_utility/14_delete_plugin.t

    r2419 r3345  
    44use warnings; 
    55 
    6 use Test::More tests => 2; 
     6use Test::More tests => 4; 
    77use Class::Hookable; 
    88 
    99my $hook = Class::Hookable->new; 
    10 my $pluginA = PluginA->new; 
    11 my $pluginB = PluginB->new; 
     10my $plugin = Plugin->new; 
    1211 
    1312$hook->register_hook( 
    14     $pluginA, 
    15     'hook.A' => $pluginA->can('foo'), 
    16     'hook.B' => $pluginA->can('bar'), 
     13    $plugin, 
     14    'hook.A' => $plugin->can('foo'), 
     15    'hook.B' => $plugin->can('foo'), 
    1716); 
    1817 
    19 $hook->register_hook( 
    20     $pluginB, 
    21     'hook.A' => $pluginB->can('foo'), 
    22     'hook.B' => $pluginB->can('bar'), 
     18$hook->register_method( 
     19    $plugin, 
     20    'method.A' => $plugin->can('bar'), 
     21    'method.B' => $plugin->can('bar'), 
    2322); 
    2423 
    25 $hook->delete_plugin( $pluginA, 'hook.B' ); 
     24$hook->delete_plugin( $plugin => qw( hook.A method.B ) ); 
    2625 
    2726is_deeply( 
    28     [ $hook->registered_hooks( $pluginA ) ], 
    29     [qw( hook.A )], 
     27    [ $hook->registered_hooks( $plugin ) ], 
     28    [qw( hook.B )], 
    3029); 
    3130 
    32 $hook->delete_plugin( $pluginB ); 
     31is_deeply( 
     32    [ $hook->registered_methods( $plugin ) ], 
     33    [qw( method.A )], 
     34); 
     35 
     36$hook->delete_plugin( $plugin ); 
    3337 
    3438is_deeply( 
    35     [ $hook->registered_hooks( 'PluginB' ) ], 
     39    [ $hook->registered_hooks( $plugin ) ], 
    3640    [], 
    3741); 
    3842 
    39 package PluginA; 
     43is_deeply( 
     44    [ $hook->registered_methods( $plugin ) ], 
     45    [], 
     46); 
     47 
     48package Plugin; 
    4049 
    4150sub new { bless {}, shift } 
    4251sub foo {} 
    4352sub bar {} 
    44  
     53sub baz {} 
    45541; 
    46  
    47 package PluginB; 
    48  
    49 sub new { bless {}, shift } 
    50 sub foo {} 
    51 sub bar {}