lib/Badger/Filter.pm

filter: Statementstmt 80.4% 74 / 92 Branchbran 66.0% 33 / 50 Conditioncond 63.1% 12 / 19 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 13 / 13 Podpod 70.0% 7 / 10 totaltotal 73.4% 144 / 196 CC 35 SCAR 22.3
CC
35
Cov
74%
init
22.9
init_filter_set
9.7
Filter
5.4
CRAP 9.3
« blib/lib/Badger/Codecs.pm ↑ Summary blib/lib/Badger/Prototype.pm »
1 package Badger::Filter;
2  
3 use Badger::Class
4 2 version => 0.01,
5 debug => 0,
6 import => 'class',
7 base => 'Badger::Base',
8 utils => 'is_object',
9 constants => 'NONE ALL TRUE FALSE CODE REGEX ARRAY HASH',
10 constant => {
11 FILTER => 'Badger::Filter',
12 },
13 exports => {
14 any => 'FILTER Filter',
15 2 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 sub Filter {
19 4 ▶ return FILTER unless @_;
Subroutine Filter: called CC=4 SCAR=16.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
20 4 ▶ return @_ == 1 && is_object(FILTER, $_[0])
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && &is_object('Badger::Filter', $_[0]) ? : 0 4 4
Condition33%1/3
@_ == 1 and &is_object("Badger::Filter", $_[0])
!ll&&!rl&&r
400
MC/DC0%0/2
@_ == 1 and &is_object("Badger::Filter", $_[0])
@_ == 1 &is_object("Badger::Filter", $_[0])
Truth table33%1/3
A and B
A= @_ == 1B= &is_object("Badger::Filter", $_[0])
ABresult
0X0
100
111
21 ? $_[0] # return existing Filter object
22 : FILTER->new(@_); # or construct a new one
23 }
24  
25 #-----------------------------------------------------------------------------
26 # Initialisation methods
27 #-----------------------------------------------------------------------------
28  
29 sub init {
30 5 ▶ my ($self, $config) = @_;
Subroutine init: called CC=11 SCAR=31.3
Pod documented
31 5 my $class = $self->class;
32 5 my $accept = $config->{ accept };
33 5 my ($include, $exclude);
34  
35 5 ▶ if ($accept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accept) { } 2 3 5
36 2 ▶ if ($accept eq ALL) {
Branch: 2 branches
BranchTrueFalseTotal
if ($accept eq 'all') { } 1 1 2
elsif ($accept eq 'none') { } 1 0 1
37 # default behaviour - no include, no exclude
38 }
39 elsif ($accept eq NONE) {
40 1 $exclude = '*';
41 }
42 else {
43 # list of "item" (include), "-item" (exclude) or "+item" (include)
44 0 my @items = split(/[^\w\-\+]+/, $accept);
45 0 my (@inc, @exc);
46  
47 0 foreach my $item (@items) {
48 0 ▶ if ($item =~ s/^\-//) { push(@exc, $item); }
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ s/^\-//) { } 0 0 0
elsif ($item =~ s/^\+//) { } 0 0 0
49 0 elsif ($item =~ s/^\+//) { push(@inc, $item); }
50 0 else { push(@inc, $item); }
51 }
52 0 ▶ $include = \@inc if @inc;
Branch: 1 branch
BranchTrueFalseTotal
if @inc 0 0 0
53 0 ▶ $exclude = \@exc if @exc;
Branch: 1 branch
BranchTrueFalseTotal
if @exc 0 0 0
54 }
55 }
56 else {
57 $include = $class->list_vars(
58 INCLUDE => $config->{ include }
59 3 );
60 $exclude = $class->list_vars(
61 EXCLUDE => $config->{ exclude }
62 3 );
63 }
64  
65 5 ▶ $self->{ include } = $self->init_filter_set(
Branch: 1 branch
BranchTrueFalseTotal
if $include 3 2 5
66 include => $include
67 ) if $include;
68  
69 5 ▶ $self->{ exclude } = $self->init_filter_set(
Branch: 1 branch
BranchTrueFalseTotal
if $exclude 4 1 5
70 exclude => $exclude
71 ) if $exclude;
72  
73 5 return $self;
74 }
75  
76 sub init_filter_set {
77 7 ▶ my ($self, $name, @items) = @_;
Subroutine init_filter_set: called CC=9 SCAR=22.7
Pod undocumented
78 7 my $static = { };
79 7 my $dynamic = [ ];
80 7 my $n = 0;
81  
82 7 $self->debug(
83 "init_filter_set($name) : ",
84 $self->dump_data(\@items)
85 ) if DEBUG;
86  
87 7 ▶ while (@items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items) 18 7 25
88 18 my $item = shift @items;
89  
90 18 ▶ if (! ref $item) {
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $item) { } 7 11 18
elsif (ref $item eq 'ARRAY') { } 6 5 11
elsif (ref $item eq 'HASH') { } 0 5 5
elsif (ref $item eq 'CODE') { } 2 3 5
elsif (ref $item eq 'Regexp') { } 3 0 3
91 7 ▶ if ((my $copy = $item) =~ s/\*/.*/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $copy = $item) =~ s/\*/.*/g) { } 2 5 7
92 2 ▶ push(@$dynamic, sub { $_[0] =~ /^$copy$/ });
Subroutine __ANON__: called CC=1 SCAR=0.0
93 2 $self->debug("$name: set wildcard item: $item") if DEBUG;
94 }
95 else {
96 5 $static->{ $item } = 1;
97 5 $self->debug("$name: set static item: $item") if DEBUG;
98 }
99 7 $n++;
100 }
101 elsif (ref $item eq ARRAY) {
102 6 unshift(@items, @$item);
103 6 $self->debug("$name: expanded array: ", $self->dump_data($item)) if DEBUG;
104 }
105 elsif (ref $item eq HASH) {
106 my $truly = {
107 0 map { @$_ } # unpack key and value
108 0 grep { $_[1] } # only accept true value
109 0 map { [$_, $item->{ $_ }] } # pack key and value
110 keys %$item
111 };
112 0 @$static{ keys %$truly } = map { 1 } values %$truly;
113 0 $n += scalar keys %$truly;
114 0 $self->debug("$name: set hash of true items: ", $self->dump_data($truly)) if DEBUG;
115 }
116 elsif (ref $item eq CODE) {
117 2 push(@$dynamic, $item);
118 2 $n++;
119 2 $self->debug("$name: added code ref: $item") if DEBUG;
120 }
121 elsif (ref $item eq REGEX) {
122 3 my $regex = $item;
123 3 ▶ push(@$dynamic, sub { $_[0] =~ $regex });
Subroutine __ANON__: called CC=1 SCAR=0.0
124 3 $n++;
125 3 $self->debug("$name: added regex: $item") if DEBUG;
126 }
127 else {
128 0 return $self->error_msg( invalid => $name => $item );
129 }
130 }
131  
132 7 ▶ return undef unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 2 5 7
133  
134 5 my $set = {
135 static => $static,
136 dynamic => $dynamic,
137 };
138  
139 5 $self->debug(
140 "init_filter_set($name) $n items: ",
141 $self->dump_data($set)
142 ) if DEBUG;
143  
144 5 return $set;
145 }
146  
147  
148 #-----------------------------------------------------------------------------
149 # List filtering methods
150 #-----------------------------------------------------------------------------
151  
152 sub accept {
153 5 ▶ my $self = shift;
Subroutine accept: called CC=4 SCAR=15.2
Pod documented
154 5 ▶ my $items = (@_ == 1 && ref $_[0] eq ARRAY) ? shift : [@_];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'ARRAY' ? : 0 5 5
Condition33%1/3
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
500
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
155 5 my @accept = grep { $self->item_accepted($_) } @$items;
156 return wantarray
157 ? @accept
158 5 ▶ : \@accept;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 0 5
159 }
160  
161 sub reject {
162 1 ▶ my $self = shift;
Subroutine reject: called CC=4 SCAR=15.2
Pod documented
163 1 ▶ my $items = (@_ == 1 && ref $_[0] eq ARRAY) ? shift : [@_];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'ARRAY' ? : 0 1 1
Condition33%1/3
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
164 1 my @reject = grep { $self->item_rejected($_) } @$items;
165 return wantarray
166 ? @reject
167 1 ▶ : \@reject;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
168 }
169  
170  
171 #-----------------------------------------------------------------------------
172 # Item filtering methods
173 #-----------------------------------------------------------------------------
174  
175 sub item_accepted {
176 77 ▶ my ($self, $item) = @_;
Subroutine item_accepted: called CC=2 SCAR=6.9
Pod documented
177 77 ▶ return $self->item_included($item)
Condition100%3/3
$self->item_included($item) && !$self->item_excluded($item)
!ll&&!rl&&r
401027
MC/DC100%2/2
$self->item_included($item) && !$self->item_excluded($item)
$self->item_included($item) !$self->item_excluded($item)
Truth table100%3/3
A && not(B)
A= $self->item_included($item)B= !$self->item_excluded($item)
ABresult
0X0
100
111
178 && !$self->item_excluded($item);
179 }
180  
181 sub item_rejected {
182 21 ▶ ! shift->item_accepted(@_);
Subroutine item_rejected: called CC=1 SCAR=0.0
Pod documented
183 }
184  
185 sub item_included {
186 77 ▶ my $self = shift;
Subroutine item_included: called CC=2 SCAR=6.9
Pod documented
187 77 ▶ my $include = $self->{ include } || return TRUE;
Condition100%2/2
$self->{'include'} || (return 1)
l!l
698
MC/DC100%1/1
$self->{'include'} || (return 1)
$self->{'include'}
Truth table100%2/2
A
A= $self->{'include'}
Aresult
11
00
188 69 return $self->check_item($include, @_);
189 }
190  
191 sub item_excluded {
192 37 ▶ my $self = shift;
Subroutine item_excluded: called CC=2 SCAR=6.9
Pod documented
193 37 ▶ my $exclude = $self->{ exclude } || return FALSE;
Condition100%2/2
$self->{'exclude'} || (return 0)
l!l
2611
MC/DC100%1/1
$self->{'exclude'} || (return 0)
$self->{'exclude'}
Truth table100%2/2
A
A= $self->{'exclude'}
Aresult
11
00
194 26 return $self->check_item($exclude, @_);
195 }
196  
197 sub check_item {
198 95 ▶ my ($self, $checks, $item) = @_;
Subroutine check_item: called CC=5 SCAR=16.1
Pod undocumented
199  
200 95 $self->debug(
201 "check [$item] via ",
202 $self->dump_data($checks)
203 ) if DEBUG;
204  
205 return TRUE
206 if $checks->{ static }->{ $item }
207 95 ▶ || $checks->{ static }->{"*"};
Branch: 1 branch
BranchTrueFalseTotal
if $checks->{"static"}{$item} or $checks->{"static"}{"*"} 10 85 95
Condition66%2/3
$checks->{"static"}{$item} or $checks->{"static"}{"*"}
l!l&&r!l&&!r
10085
MC/DC50%1/2
$checks->{"static"}{$item} or $checks->{"static"}{"*"}
$checks->{"static"}{$item} $checks->{"static"}{"*"}
Truth table66%2/3
A or B
A= $checks->{"static"}{$item}B= $checks->{"static"}{"*"}
ABresult
1X1
011
000
208  
209 85 foreach my $check (@{ $checks->{ dynamic } }) {
210 123 $self->debug("check: $check") if DEBUG;
211 123 ▶ return TRUE
Branch: 1 branch
BranchTrueFalseTotal
if &$check($item) 29 94 123
212 if $check->($item);
213 }
214  
215 56 return FALSE;
216 }
217  
218  
219 2 1;
220  
221 __END__
« blib/lib/Badger/Codecs.pm ↑ Summary blib/lib/Badger/Prototype.pm »