Changeset 3345 for lang/perl/Class-Hookable
- Timestamp:
- 12/20/07 14:12:50 (13 months ago)
- Location:
- lang/perl/Class-Hookable/trunk
- Files:
-
- 2 modified
-
lib/Class/Hookable.pm (modified) (6 diffs)
-
t/03_utility/14_delete_plugin.t (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Class-Hookable/trunk/lib/Class/Hookable.pm
r3205 r3345 202 202 for my $method ( keys %{ $self->hookable_all_methods } ) { 203 203 my $plugin = $self->hookable_all_methods->{$method}->{'plugin'}; 204 next if ( ! defined $plugin ); 204 205 my $class = ref $plugin || $plugin; 205 206 if ( $is_class ) { … … 240 241 } 241 242 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 } 246 263 } 247 264 … … 267 284 268 285 sub delete_method { 269 my ( $self, $method ) = @_;286 my ( $self, $method, @plugins ) = @_; 270 287 271 288 Carp::croak "Method name is not specified." 272 289 if ( ! defined $method ); 273 290 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 } 275 312 } 276 313 … … 293 330 294 331 sub delete_plugin { 295 my ( $self, $object, @ hooks ) = @_;332 my ( $self, $object, @points ) = @_; 296 333 297 334 if ( ref $object && ! Scalar::Util::blessed($object) ) { … … 299 336 } 300 337 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 } 322 347 } 323 348 … … 787 812 788 813 $hook->delete_plugin( $plugin ); 789 $hook->delete_plugin( ClassName => qw( hook.A hook.B) );814 $hook->delete_plugin( ClassName => qw( hook.A method.A ) ); 790 815 791 816 This method deletes a registered plugin. 792 817 793 818 A plugin object or class name is specified as the first argument, 794 and some hook names isspecified as an argument after that.819 and hook names or method names are specified as an argument after that. 795 820 796 821 When 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 hook s as arguments,800 a plugin is deleted from specified hooks .822 a plugin is deleted from all hooks and all methods. 823 824 And when specifying a plugin object (or class name) and hook names or method names as arguments, 825 a plugin is deleted from specified hooks and specified methods. 801 826 802 827 =head1 ACCESSOR METOHDS -
lang/perl/Class-Hookable/trunk/t/03_utility/14_delete_plugin.t
r2419 r3345 4 4 use warnings; 5 5 6 use Test::More tests => 2;6 use Test::More tests => 4; 7 7 use Class::Hookable; 8 8 9 9 my $hook = Class::Hookable->new; 10 my $pluginA = PluginA->new; 11 my $pluginB = PluginB->new; 10 my $plugin = Plugin->new; 12 11 13 12 $hook->register_hook( 14 $plugin A,15 'hook.A' => $plugin A->can('foo'),16 'hook.B' => $plugin A->can('bar'),13 $plugin, 14 'hook.A' => $plugin->can('foo'), 15 'hook.B' => $plugin->can('foo'), 17 16 ); 18 17 19 $hook->register_ hook(20 $plugin B,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'), 23 22 ); 24 23 25 $hook->delete_plugin( $plugin A, 'hook.B');24 $hook->delete_plugin( $plugin => qw( hook.A method.B ) ); 26 25 27 26 is_deeply( 28 [ $hook->registered_hooks( $plugin A) ],29 [qw( hook. A)],27 [ $hook->registered_hooks( $plugin ) ], 28 [qw( hook.B )], 30 29 ); 31 30 32 $hook->delete_plugin( $pluginB ); 31 is_deeply( 32 [ $hook->registered_methods( $plugin ) ], 33 [qw( method.A )], 34 ); 35 36 $hook->delete_plugin( $plugin ); 33 37 34 38 is_deeply( 35 [ $hook->registered_hooks( 'PluginB') ],39 [ $hook->registered_hooks( $plugin ) ], 36 40 [], 37 41 ); 38 42 39 package PluginA; 43 is_deeply( 44 [ $hook->registered_methods( $plugin ) ], 45 [], 46 ); 47 48 package Plugin; 40 49 41 50 sub new { bless {}, shift } 42 51 sub foo {} 43 52 sub bar {} 44 53 sub baz {} 45 54 1; 46 47 package PluginB;48 49 sub new { bless {}, shift }50 sub foo {}51 sub bar {}
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)