Changeset 2390

Show
Ignore:
Timestamp:
12/04/07 11:23:01 (5 years ago)
Author:
nyarla
Message:

lang/perl/Class-Hookable: I implemented registered_methods method and bug fix.

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

Legend:

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

    r2279 r2390  
    77use Scalar::Util(); 
    88 
    9 our $VERSION = '0.02'; 
     9use vars qw( $VERSION ); 
     10$VERSION = '0.03'; 
    1011 
    1112sub new { bless {}, shift } 
     
    171172    } 
    172173 
     174    @hooks = sort { $a cmp $b } @hooks; 
    173175    return @hooks; 
    174176} 
     
    183185 
    184186    return @{ $list }; 
     187} 
     188 
     189sub registered_methods { 
     190    my $self    = shift; 
     191    my @methods = (); 
     192 
     193    if ( @_ > 0 ) { 
     194        my $object = shift; 
     195 
     196        if ( ref $object && ! Scalar::Util::blessed($object) ) { 
     197            Carp::croak "Argument is not blessed object or class name."; 
     198        } 
     199 
     200        my $is_class = ( ! ref $object ) ? 1 : 0 ; 
     201 
     202        for my $method ( keys %{ $self->hookable_all_methods } ) { 
     203            my $plugin  = $self->hookable_all_methods->{$method}->{'plugin'}; 
     204            my $class   = ref $plugin || $plugin; 
     205            if ( $is_class ) { 
     206                push @methods, $method if ( $class eq $object ); 
     207            } 
     208            else { 
     209                push @methods, $method if ( $plugin eq $object ); 
     210            } 
     211        } 
     212    } 
     213    else { 
     214        @methods = keys %{ $self->hookable_all_methods }; 
     215    } 
     216 
     217    @methods = sort { $a cmp $b } @methods; 
     218    return @methods; 
    185219} 
    186220 
     
    606640Return value is a list of hash reference including plugin and callback. 
    607641 
     642=head2 registered_methods 
     643 
     644  my @methods = $hook->registered_methods( $plugin ); 
     645  my @methods = $hook->registered_methods( 'ClassName' ); 
     646 
     647This method returns a registered method name. 
     648 
     649When calling without arguments, all registered method name is returned. 
     650and When specifying plugin object (or class name) as an arguments, 
     651the method name with which a plugin is registered is returned. 
     652 
    608653=head2 delete_plugin 
    609654 
  • lang/perl/Class-Hookable/trunk/t/03_utility/00_registered_hooks.t

    r2120 r2390  
    2525is_deeply( 
    2626    [ $hook->registered_hooks ], 
    27     [ qw( foo.bar bar.baz baz.foo ) ], 
     27    [ qw( bar.baz baz.foo foo.bar ) ], 
    2828); 
    2929 
    3030is_deeply( 
    3131    [ $hook->registered_hooks( $pluginA ) ], 
    32     [ qw( foo.bar bar.baz ) ], 
     32    [ qw( bar.baz foo.bar) ], 
    3333); 
    3434 
    3535is_deeply( 
    3636    [ $hook->registered_hooks('PluginB') ], 
    37     [ qw(  foo.bar baz.foo ) ], 
     37    [ qw( baz.foo foo.bar ) ], 
    3838); 
    3939