File Coverage

blib/lib/App/Du/Analyze/Filter.pm
Criterion Covered Total %
statement 58 58 100.0
branch 14 16 87.5
condition 3 3 100.0
subroutine 10 10 100.0
pod 2 2 100.0
total 87 89 97.7


line stmt bran cond sub pod time code
1             package App::Du::Analyze::Filter;
2              
3 2     2   53352 use strict;
  2         3  
  2         75  
4 2     2   9 use warnings;
  2         2  
  2         1196  
5              
6             sub _my_all
7             {
8 315     315   291 my $cb = shift;
9              
10 315         401 foreach my $x (@_)
11             {
12 306 100       395 if (not $cb->(local $_ = $x))
13             {
14 162         1004 return 0;
15             }
16             }
17              
18 153         441 return 1;
19             }
20              
21             sub new
22             {
23 18     18 1 5793 my $class = shift;
24              
25 18         46 my $self = bless {}, $class;
26              
27 18         84 $self->_init(@_);
28              
29 18         59 return $self;
30             }
31              
32             sub _depth
33             {
34 36     36   44 my $self = shift;
35              
36 36 100       75 if (@_)
37             {
38 18         43 $self->{_depth} = shift;
39             }
40              
41 36         47 return $self->{_depth};
42             }
43              
44             sub _prefix
45             {
46 36     36   40 my $self = shift;
47              
48 36 100       90 if (@_)
49             {
50 18         45 $self->{_prefix} = shift;
51             }
52              
53 36         64 return $self->{_prefix};
54             }
55              
56             sub _should_sort
57             {
58 48     48   56 my $self = shift;
59              
60 48 100       84 if (@_)
61             {
62 30         42 $self->{_should_sort} = shift;
63             }
64              
65 48         57 return $self->{_should_sort};
66             }
67              
68             sub _init
69             {
70 18     18   31 my ($self, $args) = @_;
71              
72 18         58 $self->_prefix($args->{prefix});
73 18         52 $self->_depth($args->{depth});
74 18         50 $self->_should_sort(1);
75              
76 18 100       52 if (exists($args->{should_sort}))
77             {
78 12         25 $self->_should_sort($args->{should_sort});
79             }
80              
81 18         21 return;
82             }
83              
84             sub filter
85             {
86 18     18 1 12598 my ($self, $in_fh, $out_fh) = @_;
87              
88 18         45 my $prefix = $self->_prefix;
89 18         37 my $sort = $self->_should_sort;
90 18         37 my $depth = $self->_depth;
91              
92 18         30 my $compare_depth = $depth - 1;
93 18         21 my @results;
94              
95 18         40 $prefix =~ s#/+\z##;
96              
97 18         85 my @prefix_to_test = split(m#/#, $prefix);
98              
99 18         413 while(my $line = <$in_fh>)
100             {
101 6822         8385 chomp($line);
102 6822 50       34593 if (my ($size, $total_path, $path) = $line =~ m#\A(\d+)\t(\.(.*?))\z#)
103             {
104 6822         14893 my @path_to_test = split(m#/#, $total_path);
105             # Get rid of the ".".
106 6822         5788 shift(@path_to_test);
107              
108 6822 100 100     25095 if (
109             (@path_to_test == @prefix_to_test + $depth)
110             and
111 306     306   724 (_my_all (sub { $path_to_test[$_] eq $prefix_to_test[$_] }, (0 .. $#prefix_to_test)))
112             )
113             {
114 153         385 $path =~ s#\A/##;
115 153         799 push @results, [$path, $size];
116             }
117             }
118             }
119              
120 18 50       52 if ($sort)
121             {
122 18         88 @results = (sort { $a->[1] <=> $b->[1] } @results);
  393         422  
123             }
124              
125 18         40 foreach my $r (@results)
126             {
127 153         118 print {$out_fh} "$r->[1]\t$r->[0]\n";
  153         1770  
128             }
129              
130 18         169 return;
131             }
132              
133             1;
134              
135             __END__