Show
Ignore:
Timestamp:
11/28/07 12:30:04 (14 months ago)
Author:
nyarla
Message:

lang/perl/Class-Hookable: I implemented hookable_call_filter method.

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

Legend:

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

    r2097 r2100  
    7979        return $self->hookable_stash->{'filter_prefix'}; 
    8080    } 
     81} 
     82 
     83sub hookable_call_filter { 
     84    my ( $self, $name, @args ) = @_; 
     85 
     86    Carp::croak "Filter name is not specified." 
     87        if ( ! $name ); 
     88 
     89    my $prefix = $self->hookable_filter_prefix 
     90              || 'hookable_filter'; 
     91 
     92    my $filter   = $self->hookable_stash->{'filters'}->{$name}; 
     93       $filter ||= $self->can("${prefix}_${name}"); 
     94       $filter ||= sub { return 1 }; 
     95 
     96    return $filter->( $self, $name, @args ); 
    8197} 
    8298 
     
    476492=head2 hookable_call_filter 
    477493 
     494  my $bool = $hook->hookable_call_filter( $name => @args ); 
     495 
     496This method calls a specified filter. 
     497 
     498A filter name is specified as the first argument 
     499and an argument to a filter is specified as an argument after that. 
     500 
     501B<Search of filter>: 
     502 
     503This method searches for a filter from several places. 
     504 
     505First, when a specified filter is specified by C<$hook-E<gt>hookable_set_filter> method,  
     506the filter is used. 
     507 
     508Next when C<$hook-E<gt>can("${prefix}_${filter_name}")> is defined, 
     509its method is used as a filter. 
     510 
     511C<${prefix}> is return value of $hook->hookable_filter_prefix, 
     512and C<${filter_name}> is the filter name specified as this method. 
     513 
     514When C<$hook-E<gt>hookble_filter_perfix> is not specified,  
     515C<${prefix}> will be C<'hookable_filter'>. 
     516 
     517Please see L<"hookable_filter_prefix"> about C<$hook-E<gt>hookable_filter_prefix>. 
     518 
     519When a filter wasn't found, this method uses the filter to which truth is just always returned. 
     520 
    478521B<Arguments of filter>: 
    479522 
     523  $hook->hookable_set_filter( 
     524      'run_hook' => sub { 
     525          my ( $hook, $filter, @args ) = @_; 
     526      }, 
     527  ); 
     528 
    480529=over 3 
    481530 
    482 =item register_hook / register_method 
    483  
    484 =item run_hook (run_hook_once) 
    485  
    486 =item call_method 
     531=item C<$hook> 
     532 
     533Instance of Class::Hookable (or the class inheriting to Class::Hookable). 
     534 
     535=item C<$filter> 
     536 
     537The filter name called in C<$hook-E<gt>hookable_call_filter>. 
     538 
     539=item C<@args> 
     540 
     541Arguments to the filter to which it was passed by C<$hook-E<gt>hookable_call_filter>. 
    487542 
    488543=back