Assets/Filter.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 205 CC 17 SCAR 32.2
CC
17
Cov
0%
new_parse
90.0
filter
20.0
fit
12.0
CRAP 24.9
1 package File::Assets::Filter;
2  
3 use strict;
4 use warnings;
5  
6 use Object::Tiny qw/cfg where signature/;
7 use File::Assets::Carp;
8  
9 use Digest;
10 use Scalar::Util qw/weaken/;
11  
12 for my $ii (qw/matched mtime assets bucket slice/) {
13 no strict 'refs';
14 *$ii = sub {
15 my $self = shift;
16 return $self->stash->{$ii} unless @_;
17 return $self->stash->{$ii} = shift;
18 };
19 }
20  
21 my %default = (qw/
22 /,
23 output => undef,
24 );
25  
26 sub new_parse {
27 my $class = shift;
28 return unless my $filter = shift;
29  
30 $filter =~ s/^yui-compressor\b/yuicompressor/; # A special case
31 my $kind = lc $class;
32 $kind =~ s/^File::Assets::Filter:://i;
33 $kind =~ s/::/-/g;
34  
35 my %cfg;
36 if (ref $filter eq "") {
37 my $cfg = $filter;
38 return unless $cfg =~ s/^\s*$kind(?:\s*$|:([^:]))//i;
39 $cfg = "$1$cfg" if defined $1;
40 %cfg = $class->new_parse_cfg($cfg);
41 if (ref $_[0] eq "HASH") {
42 %cfg = (%cfg, %{ $_[0] });
43 shift;
44 }
45 elsif (ref $_[0] eq "ARRAY") {
46 %cfg = (%cfg, @{ $_[0] });
47 shift;
48 }
49 }
50 elsif (ref $filter eq "ARRAY") {
51 # TODO? Get rid of this?
52 return unless $filter->[0] && $filter->[0] =~ m/^\s*$kind\s*$/i;
53 my @cfg = @$filter;
54 shift @cfg;
55 %cfg = @cfg;
56 }
57  
58 return $class->new(%cfg, @_);
59 }
60  
61 sub new_parse_cfg {
62 my $class = shift;
63 my $cfg = shift;
64 $cfg = "" unless defined $cfg;
65 my %cfg;
66 %cfg = map { my @itm = split m/=/, $_, 2; $itm[0], $itm[1] } split m/;/, $cfg;
67 $cfg{__cfg__} = $cfg;
68 return %cfg;
69 }
70  
71 sub new {
72 my $class = shift;
73 my $self = $class->SUPER::new;
74 local %_ = @_;
75  
76 # $self->{assets} = $_{assets};
77 # weaken $self->{assets};
78  
79 $self->{cfg} = {};
80  
81 while (my ($setting, $value) = each %default) {
82 $self->cfg->{$setting} = exists $_{$setting} ? $_{$setting} : $value;
83 }
84  
85 return $self;
86 }
87  
88 sub fit {
89 my $self = shift;
90 my $bucket = shift;
91  
92 return 1 unless $self->{fit};
93 return 1 if $bucket->kind->is_better_than_or_equal($self->{fit});
94 }
95  
96 sub stash {
97 return shift->{stash} ||= {};
98 }
99  
100 sub type {
101 return shift->where->{type};
102 }
103  
104 sub output {
105 return shift->cfg->{output};
106 }
107  
108 sub begin {
109 my $self = shift;
110 my $slice = shift;
111 my $bucket = shift;
112 my $assets = shift;
113  
114 $self->stash->{slice} = $slice;
115 $self->stash->{bucket} = $bucket;
116 $self->stash->{assets} = $assets;
117 $self->stash->{mtime} = 0;
118 }
119  
120 sub end {
121 my $self = shift;
122 delete $self->{stash};
123 }
124  
125 sub filter {
126 my $self = shift;
127 my $slice = shift;
128 my $bucket = shift;
129 my $assets = shift;
130  
131 $self->begin($slice, $bucket, $assets);
132  
133 return unless $self->pre;
134  
135 my @matched;
136 $self->matched(\@matched);
137  
138 my $count = 0;
139 for (my $rank = 0; $rank < @$slice; $rank++) {
140 my $asset = $slice->[$rank];
141  
142 next unless $self->_match($asset);
143  
144 $count = $count + 1;
145 push @matched, { asset => $asset, rank => $rank, count => $count };
146  
147 my $asset_file_mtime = $asset->file_mtime;
148 $self->mtime($asset_file_mtime) if $asset_file_mtime >= $self->mtime;
149  
150 $self->process($asset, $rank, $count, scalar @$slice, $slice);
151 }
152 $self->post;
153  
154 $self->end;
155 }
156  
157 sub _match {
158 my $self = shift;
159 my $asset = shift;
160  
161 return $self->match($asset, 1);
162 }
163  
164 sub match {
165 my $self = shift;
166 my $asset = shift;
167 my $match = shift;
168  
169 return $match ? 1 : 0;
170 }
171  
172 sub pre {
173 return 1;
174 }
175  
176 sub process {
177 }
178  
179 sub post {
180 return 1;
181 }
182  
183 sub remove {
184 my $self = shift;
185 carp __PACKAGE__, "::remove() is deprecated, nothing happens";
186 return;
187 }
188  
189 sub kind {
190 my $self = shift;
191 return $self->bucket->kind;
192 }
193  
194 1;
195  
196 __END__