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