blib/lib/Badger/Filter.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 205 CC 35 SCAR 48.5
CC
35
Cov
0%
init_filter_set
272.0
init
110.0
check_item
30.0
CRAP 127.1
1 package Badger::Filter;
2  
3 use Badger::Class
4 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 };
16  
17  
18 sub Filter {
19 return FILTER unless @_;
20 return @_ == 1 && is_object(FILTER, $_[0])
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 my ($self, $config) = @_;
31 my $class = $self->class;
32 my $accept = $config->{ accept };
33 my ($include, $exclude);
34  
35 if ($accept) {
36 if ($accept eq ALL) {
37 # default behaviour - no include, no exclude
38 }
39 elsif ($accept eq NONE) {
40 $exclude = '*';
41 }
42 else {
43 # list of "item" (include), "-item" (exclude) or "+item" (include)
44 my @items = split(/[^\w\-\+]+/, $accept);
45 my (@inc, @exc);
46  
47 foreach my $item (@items) {
48 if ($item =~ s/^\-//) { push(@exc, $item); }
49 elsif ($item =~ s/^\+//) { push(@inc, $item); }
50 else { push(@inc, $item); }
51 }
52 $include = \@inc if @inc;
53 $exclude = \@exc if @exc;
54 }
55 }
56 else {
57 $include = $class->list_vars(
58 INCLUDE => $config->{ include }
59 );
60 $exclude = $class->list_vars(
61 EXCLUDE => $config->{ exclude }
62 );
63 }
64  
65 $self->{ include } = $self->init_filter_set(
66 include => $include
67 ) if $include;
68  
69 $self->{ exclude } = $self->init_filter_set(
70 exclude => $exclude
71 ) if $exclude;
72  
73 return $self;
74 }
75  
76 sub init_filter_set {
77 my ($self, $name, @items) = @_;
78 my $static = { };
79 my $dynamic = [ ];
80 my $n = 0;
81  
82 $self->debug(
83 "init_filter_set($name) : ",
84 $self->dump_data(\@items)
85 ) if DEBUG;
86  
87 while (@items) {
88 my $item = shift @items;
89  
90 if (! ref $item) {
91 if ((my $copy = $item) =~ s/\*/.*/g) {
92 push(@$dynamic, sub { $_[0] =~ /^$copy$/ });
93 $self->debug("$name: set wildcard item: $item") if DEBUG;
94 }
95 else {
96 $static->{ $item } = 1;
97 $self->debug("$name: set static item: $item") if DEBUG;
98 }
99 $n++;
100 }
101 elsif (ref $item eq ARRAY) {
102 unshift(@items, @$item);
103 $self->debug("$name: expanded array: ", $self->dump_data($item)) if DEBUG;
104 }
105 elsif (ref $item eq HASH) {
106 my $truly = {
107 map { @$_ } # unpack key and value
108 grep { $_[1] } # only accept true value
109 map { [$_, $item->{ $_ }] } # pack key and value
110 keys %$item
111 };
112 @$static{ keys %$truly } = map { 1 } values %$truly;
113 $n += scalar keys %$truly;
114 $self->debug("$name: set hash of true items: ", $self->dump_data($truly)) if DEBUG;
115 }
116 elsif (ref $item eq CODE) {
117 push(@$dynamic, $item);
118 $n++;
119 $self->debug("$name: added code ref: $item") if DEBUG;
120 }
121 elsif (ref $item eq REGEX) {
122 my $regex = $item;
123 push(@$dynamic, sub { $_[0] =~ $regex });
124 $n++;
125 $self->debug("$name: added regex: $item") if DEBUG;
126 }
127 else {
128 return $self->error_msg( invalid => $name => $item );
129 }
130 }
131  
132 return undef unless $n;
133  
134 my $set = {
135 static => $static,
136 dynamic => $dynamic,
137 };
138  
139 $self->debug(
140 "init_filter_set($name) $n items: ",
141 $self->dump_data($set)
142 ) if DEBUG;
143  
144 return $set;
145 }
146  
147  
148 #-----------------------------------------------------------------------------
149 # List filtering methods
150 #-----------------------------------------------------------------------------
151  
152 sub accept {
153 my $self = shift;
154 my $items = (@_ == 1 && ref $_[0] eq ARRAY) ? shift : [@_];
155 my @accept = grep { $self->item_accepted($_) } @$items;
156 return wantarray
157 ? @accept
158 : \@accept;
159 }
160  
161 sub reject {
162 my $self = shift;
163 my $items = (@_ == 1 && ref $_[0] eq ARRAY) ? shift : [@_];
164 my @reject = grep { $self->item_rejected($_) } @$items;
165 return wantarray
166 ? @reject
167 : \@reject;
168 }
169  
170  
171 #-----------------------------------------------------------------------------
172 # Item filtering methods
173 #-----------------------------------------------------------------------------
174  
175 sub item_accepted {
176 my ($self, $item) = @_;
177 return $self->item_included($item)
178 && !$self->item_excluded($item);
179 }
180  
181 sub item_rejected {
182 ! shift->item_accepted(@_);
183 }
184  
185 sub item_included {
186 my $self = shift;
187 my $include = $self->{ include } || return TRUE;
188 return $self->check_item($include, @_);
189 }
190  
191 sub item_excluded {
192 my $self = shift;
193 my $exclude = $self->{ exclude } || return FALSE;
194 return $self->check_item($exclude, @_);
195 }
196  
197 sub check_item {
198 my ($self, $checks, $item) = @_;
199  
200 $self->debug(
201 "check [$item] via ",
202 $self->dump_data($checks)
203 ) if DEBUG;
204  
205 return TRUE
206 if $checks->{ static }->{ $item }
207 || $checks->{ static }->{"*"};
208  
209 foreach my $check (@{ $checks->{ dynamic } }) {
210 $self->debug("check: $check") if DEBUG;
211 return TRUE
212 if $check->($item);
213 }
214  
215 return FALSE;
216 }
217  
218  
219 1;
220  
221 __END__