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

lang/perl/Class-Hookable: I rewrote register_hook and run_hook so that hookable_call_filter might be used.

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

Legend:

Unmodified
Added
Removed
  • lang/perl/Class-Hookable/trunk/Changes

    r2020 r2101  
    330.03  
    44        * Substantial change * 
    5         * renamed method 
    6           context -> hookable_context 
    7         * added new method 
    8           hookable_stash        : stash in Class::Hookable. 
    9           hookable_set_filter   : 
     5        * Change in the filtering of plugin 
     6          It was changed substantially about a way of filtering of a plugin.         
     7          It was possible to rewrite a filter only by inheriting of Class::Hookable before, 
     8          but this change could change it now every instance. 
     9          The previous filter_register_hook and filter_run_hook method 
     10          weren't used any more by this change. 
     11          added methods: 
     12            hookable_set_filter 
     13            hookable_call_filter 
     14            hookable_filter_prefix 
    1015 
    11160.02  Wed Nov 11 13:00:00 JST 2007 
  • lang/perl/Class-Hookable/trunk/lib/Class/Hookable.pm

    r2100 r2101  
    112112        }; 
    113113 
    114         if ( $self->filter_register_hook( $hook, $action ) ) { 
     114        if ( $self->hookable_call_filter( 'register_hook', $hook, $action ) ) { 
    115115            $self->hooks->{$hook} = [] 
    116116                if ( ref $self->hooks->{$hook} ne 'ARRAY' ); 
     
    120120    } 
    121121} 
    122  
    123 sub filter_register_hook { 1 } 
    124122 
    125123sub registered_hooks { 
     
    218216 
    219217    for my $action ( $self->registered_plugins( $hook ) ) { 
    220         if ( $self->filter_run_hook( $hook, $args, $action ) ) { 
     218        if ( $self->hookable_call_filter( 'run_hook', $hook, $args, $action ) ) { 
    221219            my $plugin = $action->{'plugin'}; 
    222220            my $result = $action->{'callback'}->( $plugin, $context, $args ); 
     
    239237    return $self->run_hook( $hook, $args, 1, $callback ); 
    240238} 
    241  
    242 sub filter_run_hook { 1 } 
    243239 
    2442401; 
     
    271267I thank Tatsuhiko Miyagawa and Plagger contributors. 
    272268 
    273 B<NOTE>: 
    274  
    275 B<I made substantial changes in Class::Hookable> from 0.02 to 0.03. 
    276  
    277 When using Class::Hookable, B<please be careful of Class::Hookable version>. 
    278  
    279269=head1 BASIC METHOD 
    280270 
     
    310300the subroutine registered with hook and method. 
    311301 
    312 see also L<"run_hook"> and L<"call_method">. 
     302see also L<"run_hook">. 
    313303 
    314304=head1 REGISTER METOHDS 
     
    327317and one after that is specified by the order of C<'hook' =E<gt> \&callabck>. 
    328318 
    329 =head2 filter_register_hook 
    330  
    331   sub filter_register_hook { 
    332       my ( $self, $hook, $action ) = @_; 
    333       my ( $plugin, $callback ) = @{ $action }{qw( plugin callback )}; 
    334       # your filter code 
    335   } 
    336  
    337 When registering a plugin, this method is filtered a plugin. 
    338 Arguments are passed by the order of C<$hook> and C<$action>. 
    339  
    340 =over 2 
    341  
    342 =item C<$hook> 
    343  
    344 The hook name specified as the run_hook method. 
    345  
    346 =item C<$action> 
     319Only when C<$hook-E<gt>hookable_call_filter( 'run_hook', $hook, $action )> has returned truth, 
     320the callback specified by this method is registered with a hook. 
     321 
     322Please see L<"hookable_call_filter"> about C<$hook-E<gt>hookable_call_filter>. 
     323 
     324B<Arguments of C<$hook-E<gt>hookable_call_filter>>: 
     325 
     326  $hook->hookable_call_filter( 'run_hook', $hook, $action ); 
     327 
     328=over 3 
     329 
     330=item 'run_hook' 
     331 
     332C<'run_hook'> is filter name. 
     333 
     334=item $hook 
     335 
     336The hook name specified as the register_hook method. 
     337 
     338=item $action 
     339 
     340  my ( $plugin, $callback ) = @{ $action }{qw( plugin callback )}; 
    347341 
    348342The hash reference including plugin and callback. 
    349343 
    350344=back 
    351  
    352 When this method has returned ture, plugin and hook are registered, 
    353 and when having returned false, it isn't registered. 
    354  
    355 This method exists to rewrite when inheriting. 
    356345 
    357346=head1 CALL METHODS 
     
    403392=back 
    404393 
    405 B<Argument to callback of the registered plugin>: 
     394B<Arguments of registered callback>: 
    406395 
    407396  sub callback { 
     
    433422=back 
    434423 
     424 
     425Only when C<$hook-E<gt>hookable_call_filter( 'run_hook', $hook, $args, $action )> has returned truth, 
     426this method calls callback. 
     427 
     428Please see L<"hookable_call_filter"> about C<$hook-E<gt>hookable_call_filter>. 
     429 
     430B<Arguments of C<$hook-E<gt>hookable_call_filter>>: 
     431 
     432  $hook->hookable_call_filter( 'run_hook', $hook, $args, $action ); 
     433 
     434=over 4 
     435 
     436=item 'run_hook' 
     437 
     438C<'run_hook'> is filter name. 
     439 
     440=item C<$hook> 
     441 
     442The hook name specified by the run_hook method. 
     443 
     444=item C<$args> 
     445 
     446The argument specified by the run_hook method. 
     447 
     448=item C<$action> 
     449 
     450  my ( $plugin, $callback ) = @{ $action }{qw( plugin callback )}; 
     451 
     452The hash reference including the plugin and the callback. 
     453 
     454=back 
     455 
    435456=head2 run_hook_once 
    436457 
     
    438459 
    439460This method is an alias of C<$hook-E<gt>run_hook( $hook, $args, 1, \&callback )>. 
    440  
    441 =head2 filter_run_hook 
    442  
    443   sub filter_run_hook { 
    444       my ( $self, $hook, $args, $action ) = @_; 
    445       my ( $plugin, $callabck ) = @{ $action }{qw( plugin callback )}; 
    446       # some code 
    447   } 
    448  
    449 When calling a hook, this method is filtered a plugin. 
    450 Argument are passed by the order of C<$hook>, C<$args>, C<$action>. 
    451  
    452 =over 3 
    453  
    454 =item C<$hook> 
    455  
    456 The hook name specified by the run_hook method. 
    457  
    458 =item C<$args> 
    459  
    460 The argument specified by the run_hook method. 
    461  
    462 =item C<$action> 
    463  
    464 The hash reference including the plugin and the callback. 
    465  
    466 =back 
    467  
    468 When this method has returned true, callback of a plugin is called,  
    469 and when having returned false, callback isn't called. 
    470  
    471 This method exists to rewrite when inheriting. 
    472461 
    473462=head1 FILTER METHODS 
  • lang/perl/Class-Hookable/trunk/t/02_register/00_register_hook.t

    r1878 r2101  
    44use warnings; 
    55 
    6 use Test::More tests => 3; 
     6use Test::More tests => 7; 
    77use Class::Hookable; 
    88 
     
    2525); 
    2626 
    27 { 
    28     no warnings 'redefine'; 
    29     *Class::Hookable::filter_register_hook = sub { 0 }; 
    30 } 
     27$hook->hookable_set_filter( 
     28    'register_hook' => sub { 
     29        my ( $self, $filter, $hook, $action ) = @_; 
     30        isa_ok( $self, 'Class::Hookable' ); 
     31        is( $filter, 'register_hook' ); 
     32        is( $hook, 'AAA.BBB' ); 
     33        is_deeply( 
     34            $action, 
     35            { 
     36                plugin      => $plugin, 
     37                callback    => $plugin->can('bar'), 
     38            }, 
     39        ); 
     40    }, 
     41); 
    3142 
    3243$hook->register_hook( 
  • lang/perl/Class-Hookable/trunk/t/04_call/00_run_hook.t

    r1959 r2101  
    44use warnings; 
    55 
    6 use Test::More tests => 5 + 2 + 4; 
     6use Test::More tests => 5 + 2 + 6; 
    77use Class::Hookable; 
    88 
     
    6262# -- dispatch_plugin test ------------ # 
    6363 
    64 no warnings 'redefine'; 
    65 *Class::Hookable::filter_run_hook = sub { 
    66     my ( $self, $hook, $args, $action ) = @_; 
    6764 
    68     is( $hook, 'dispatch' ); 
    69     is( $args, undef ); 
    70     is_deeply( 
    71         $action, 
    72         { 
    73             plugin      => $plugin, 
    74             callback    => $plugin->can('foo'), 
    75         } 
    76     ); 
     65$hook->hookable_set_filter( 
     66    'run_hook' => sub { 
     67        my ( $self, $filter, $hook, $args, $action ) = @_; 
    7768 
    78     return 0; 
    79 }; 
     69        isa_ok( $self, 'Class::Hookable' ); 
     70        is( $filter, 'run_hook' ); 
     71        is( $hook, 'dispatch' ); 
     72        is( $args, undef ); 
     73        is_deeply( 
     74            $action, 
     75            { 
     76                plugin      => $plugin, 
     77                callback    => $plugin->can('foo'), 
     78            } 
     79        ); 
     80 
     81        return 0; 
     82    }, 
     83); 
    8084 
    8185is(