File Coverage

blib/lib/App/Ack/Filter.pm
Criterion Covered Total %
statement 15 26 57.6
branch 0 2 0.0
condition n/a
subroutine 5 10 50.0
pod 6 6 100.0
total 26 44 59.0


line stmt bran cond sub pod time code
1             package App::Ack::Filter;
2              
3 2     2   13 use strict;
  2         4  
  2         59  
4 2     2   10 use warnings;
  2         3  
  2         49  
5              
6 2     2   10 use App::Ack ();
  2         4  
  2         37  
7 2     2   982 use App::Ack::Filter::Inverse ();
  2         6  
  2         518  
8              
9             my %filter_types;
10              
11             =head1 NAME
12              
13             App::Ack::Filter - Filter objects to filter files
14              
15             =head1 DESCRIPTION
16              
17             An abstract superclass that represents objects that can filter
18             C objects. App::Ack::Filter implementations are
19             responsible for filtering filenames to be searched.
20              
21             =head1 SYNOPSIS
22              
23             # filter implementation
24             package MyFilter;
25              
26             use strict;
27             use warnings;
28             use parent 'App::Ack::Filter';
29              
30             sub filter {
31             my ( $self, $file ) = @_;
32             }
33              
34             BEGIN {
35             App::Ack::Filter->register_filter('mine' => __PACKAGE__);
36             }
37              
38             1;
39              
40             # users
41             App::Ack::Filter->create_filter('mine', @args);
42              
43              
44             =head1 METHODS
45              
46             =head2 App::Ack::Filter->create_filter($type, @args)
47              
48             Creates a filter implementation, registered as C<$type>. C<@args>
49             are provided as additional arguments to the implementation's constructor.
50              
51             =cut
52              
53             sub create_filter {
54 0     0 1 0 my ( undef, $type, @args ) = @_;
55              
56 0 0       0 if ( my $package = $filter_types{$type} ) {
57 0         0 return $package->new(@args);
58             }
59 0         0 my $allowed_types = join( ', ', sort keys %filter_types );
60 0         0 App::Ack::die( "Unknown filter type '$type'. Type must be one of: $allowed_types." );
61             }
62              
63             =head2 App::Ack::Filter->register_filter($type, $package)
64              
65             Registers a filter implementation package C<$package> under
66             the name C<$type>.
67              
68             =cut
69              
70             sub register_filter {
71 8     8 1 30 my ( undef, $type, $package ) = @_;
72              
73 8         30 $filter_types{$type} = $package;
74              
75 8         224 return;
76             }
77              
78             =head2 $filter->filter( $file )
79              
80             Must be implemented by filter implementations. Returns
81             true if the filter passes, false otherwise. This method
82             must B alter the passed-in C<$file> object.
83              
84             =head2 $filter->invert()
85              
86             Returns a filter whose L method returns the opposite of this filter.
87              
88             =cut
89              
90             sub invert {
91 0     0 1   my ( $self ) = @_;
92              
93 0           return App::Ack::Filter::Inverse->new( $self );
94             }
95              
96             =head2 $filter->is_inverted()
97              
98             Returns true if this filter is an inverted filter; false otherwise.
99              
100             =cut
101              
102             sub is_inverted {
103 0     0 1   return 0;
104             }
105              
106             =head2 $filter->to_string
107              
108             Converts the filter to a string. This method is also
109             called implicitly by stringification.
110              
111             =cut
112              
113             sub to_string {
114 0     0 1   return '(unimplemented to_string)';
115             }
116              
117             =head2 $filter->inspect
118              
119             Prints a human-readable debugging string for this filter. Useful for,
120             you guessed it, debugging.
121              
122             =cut
123              
124             sub inspect {
125 0     0 1   my ( $self ) = @_;
126              
127 0           return ref($self);
128             }
129              
130             1;