Show
Ignore:
Timestamp:
11/28/07 17:33:25 (7 years ago)
Author:
nyarla
Message:

lang/perl/Class-Hookable: I changed the specification of the registered_hooks method.

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

Legend:

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

    r2102 r2120  
    115115 
    116116sub registered_hooks { 
    117     my ( $self, $object ) = @_; 
    118  
    119     if ( ref $object && ! Scalar::Util::blessed( $object ) ) { 
    120         Carp::croak "Argument is not blessed object or class name."; 
    121     } 
    122  
    123     my $is_class = ( ! ref $object ) ? 1 : 0 ; 
    124     my @hooks = (); 
    125  
    126     for my $hook ( keys %{ $self->hookable_all_hooks } ) { 
    127         for my $action ( @{ $self->hookable_all_hooks->{$hook} } ) { 
    128             my $plugin = $action->{'plugin'}; 
    129             my $class  = ref $plugin || $plugin; 
    130             if ( $is_class ) { 
    131                 push @hooks, $hook if ( $class eq $object ); 
    132             } 
    133             else { 
    134                 push @hooks, $hook if ( $plugin eq $object ); 
     117    my $self = shift; 
     118 
     119    my @hooks; 
     120 
     121    if ( @_ > 0 ) { 
     122        my $object = shift; 
     123 
     124        if ( ref $object && ! Scalar::Util::blessed( $object ) ) { 
     125            Carp::croak "Argument is not blessed object or class name."; 
     126        } 
     127 
     128        my $is_class = ( ! ref $object ) ? 1 : 0 ; 
     129 
     130        for my $hook ( keys %{ $self->hookable_all_hooks } ) { 
     131            for my $action ( @{ $self->hookable_all_hooks->{$hook} } ) { 
     132                my $plugin = $action->{'plugin'}; 
     133                my $class  = ref $plugin || $plugin; 
     134                if ( $is_class ) { 
     135                    push @hooks, $hook if ( $class eq $object ); 
     136                } 
     137                else { 
     138                    push @hooks, $hook if ( $plugin eq $object ); 
     139                } 
    135140            } 
    136141        } 
     142    } 
     143    else { 
     144        @hooks = keys %{ $self->hookable_all_hooks }; 
    137145    } 
    138146 
     
    516524  my @hooks = $hook->registered_hooks( 'ClassName' ); 
    517525 
    518 This method returns hooks with which a plugin is registered. 
    519 An argument is plugin object or class name. 
     526This method returns a registered hook name. 
     527 
     528When calling without arguments, all registered hook name is returned. 
     529and when specifying plugin obejct (or Class name) as an argument, 
     530the hook name with which a plugin is registered is returned. 
    520531 
    521532=head2 registered_plugins 
     
    532543 
    533544  $hook->delete_plugin( $plugin ); 
    534   $hook->delete_plugin( 'ClassName', 'hook.A', 'hook.B' ); 
     545  $hook->delete_plugin( ClassName => qw( hook.A hook.B ) ); 
    535546 
    536547This method delete a registered plugin. 
     
    545556 
    546557  $hook->delete_hook( 'hook.name' ); 
    547   $hook->delete_hook( 'hook.name', $pluginA, 'ClassName' ); 
     558  $hook->delete_hook( 'hook.name' => ( $pluginA, 'ClassName' ) ); 
    548559 
    549560This method delete a registered hook. 
  • lang/perl/Class-Hookable/trunk/t/03_utility/00_registered_hooks.t

    r1876 r2120  
    44use warnings; 
    55 
    6 use Test::More tests => 2; 
     6use Test::More tests => 3; 
    77use Class::Hookable; 
    88 
     
    2121    'foo.bar' => $pluginB->can('foo'), 
    2222    'baz.foo' => $pluginB->can('bar'), 
     23); 
     24 
     25is_deeply( 
     26    [ $hook->registered_hooks ], 
     27    [ qw( foo.bar bar.baz baz.foo ) ], 
    2328); 
    2429