line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Pipeline::Action::Filter; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
2478
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
with 'Data::Pipeline::Action'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use MooseX::Types::Moose qw(RegexpRef CodeRef); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Data::Pipeline::Types qw( Iterator ); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has filter => ( |
11
|
|
|
|
|
|
|
isa => 'CodeRef', |
12
|
|
|
|
|
|
|
is => 'rw', |
13
|
|
|
|
|
|
|
lazy => 1, |
14
|
|
|
|
|
|
|
predicate => 'has_filter', |
15
|
|
|
|
|
|
|
default => sub { sub { $_[0] } } |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# filters => { title => qr/^\[(video|audiobook|game)\]/ } |
19
|
|
|
|
|
|
|
# reject_matching => 1 |
20
|
|
|
|
|
|
|
has filters => ( |
21
|
|
|
|
|
|
|
isa => 'HashRef', |
22
|
|
|
|
|
|
|
is => 'ro', |
23
|
|
|
|
|
|
|
lazy => 1, |
24
|
|
|
|
|
|
|
predicate => 'has_filters', |
25
|
|
|
|
|
|
|
default => sub { +{ } } |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has reject_matching => ( |
29
|
|
|
|
|
|
|
isa => 'Bool', |
30
|
|
|
|
|
|
|
is => 'ro', |
31
|
|
|
|
|
|
|
default => 0 |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has require_all => ( |
35
|
|
|
|
|
|
|
isa => 'Bool', |
36
|
|
|
|
|
|
|
is => 'ro', |
37
|
|
|
|
|
|
|
default => 1 |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub BUILD { |
41
|
|
|
|
|
|
|
my($self) = @_; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
if(! $self -> has_filter && $self -> has_filters ) { |
44
|
|
|
|
|
|
|
# print "BUilding a filter\n"; |
45
|
|
|
|
|
|
|
my %filters = %{$self -> filters}; |
46
|
|
|
|
|
|
|
$self -> filter(sub { |
47
|
|
|
|
|
|
|
my $item = $_[0]; |
48
|
|
|
|
|
|
|
my $match = 0; |
49
|
|
|
|
|
|
|
foreach my $f ( keys %filters ) { |
50
|
|
|
|
|
|
|
if( is_RegexpRef( $filters{$f} ) ) { |
51
|
|
|
|
|
|
|
no warnings; |
52
|
|
|
|
|
|
|
$match = !!($item->{$f} =~ $filters{$f}); |
53
|
|
|
|
|
|
|
# print "$f: $filters{$f} == $match\n"; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
elsif( is_CodeRef( $filters{$f} ) ) { |
56
|
|
|
|
|
|
|
$match = !!($filters{$f} -> ($item->{$f})); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
|
|
|
|
|
|
$match = $item->{$f} eq $filters{$f}; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
return 0 if $self -> require_all && ($match == $self -> reject_matching); |
63
|
|
|
|
|
|
|
return 1 if !$self -> require_all && ($match != $self -> reject_matching); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
return $self -> require_all; |
67
|
|
|
|
|
|
|
}); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub transform { |
72
|
|
|
|
|
|
|
my($self, $iterator) = @_; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
$iterator = to_Iterator($iterator); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my($next, $has_next); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
while( !$iterator -> finished ) { |
79
|
|
|
|
|
|
|
$next = $iterator -> next; |
80
|
|
|
|
|
|
|
$has_next = 1; |
81
|
|
|
|
|
|
|
last if $self -> filter -> ($next); |
82
|
|
|
|
|
|
|
$next = undef; |
83
|
|
|
|
|
|
|
$has_next = 0; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
return Data::Pipeline::Iterator -> new( |
87
|
|
|
|
|
|
|
source => Data::Pipeline::Iterator::Source -> new( |
88
|
|
|
|
|
|
|
has_next => sub { |
89
|
|
|
|
|
|
|
$has_next; |
90
|
|
|
|
|
|
|
}, |
91
|
|
|
|
|
|
|
get_next => sub { |
92
|
|
|
|
|
|
|
my $ret = $next; |
93
|
|
|
|
|
|
|
while( !$iterator -> finished ) { |
94
|
|
|
|
|
|
|
$next = $iterator -> next; |
95
|
|
|
|
|
|
|
$has_next = 1; |
96
|
|
|
|
|
|
|
last if $self -> filter -> ($next); |
97
|
|
|
|
|
|
|
$next = undef; |
98
|
|
|
|
|
|
|
$has_next = 0; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
$has_next = 0 if $iterator -> finished; |
101
|
|
|
|
|
|
|
$iterator = undef unless $has_next; |
102
|
|
|
|
|
|
|
return $ret; |
103
|
|
|
|
|
|
|
}, |
104
|
|
|
|
|
|
|
) |
105
|
|
|
|
|
|
|
); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
__END__ |