forked from beyondgrep/ack2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collection.pm
62 lines (46 loc) · 1.02 KB
/
Collection.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package App::Ack::Filter::Collection;
use strict;
use warnings;
use base 'App::Ack::Filter';
sub new {
my ( $class ) = @_;
return bless {
groups => {},
ungrouped => [],
}, $class;
}
sub filter {
my ( $self, $resource ) = @_;
for my $group (values %{$self->{'groups'}}) {
if ($group->filter($resource)) {
return 1;
}
}
for my $filter (@{$self->{'ungrouped'}}) {
if ($filter->filter($resource)) {
return 1;
}
}
return 0;
}
sub add {
my ( $self, $filter ) = @_;
if (exists $filter->{'groupname'}) {
my $group = ($self->{groups}->{$filter->{groupname}} ||= $filter->create_group());
$group->add($filter);
}
else {
push @{$self->{'ungrouped'}}, $filter;
}
return;
}
sub inspect {
my ( $self ) = @_;
return ref($self) . " - $self";
}
sub to_string {
my ( $self ) = @_;
my $ungrouped = $self->{'ungrouped'};
return join(', ', map { "($_)" } @{$ungrouped});
}
1;