Assets/Util.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 258 CC 23 SCAR 32.4
CC
23
Cov
0%
build_output_path
72.0
parse_rsc
42.0
parse_type
30.0
CRAP 25.6
1 package File::Assets::Util;
2  
3 use strict;
4 use warnings;
5  
6 use File::Assets::Carp;
7  
8 use MIME::Types();
9 use Scalar::Util qw/blessed/;
10 use Module::Pluggable search_path => q/File::Assets::Filter/, require => 1, sub_name => q/filter_load/;
11 use Digest;
12 use File::Assets::Asset;
13  
14 {
15 my $types;
16 sub types {
17 return $types ||= MIME::Types->new(only_complete => 1);
18 }
19 }
20  
21 sub digest {
22 return Digest->new("MD5");
23 }
24  
25 sub parse_name {
26 my $class = shift;
27 my $name = shift;
28 $name = "" unless defined $name;
29 $name = $name."";
30 return undef unless length $name;
31 return $name;
32 }
33  
34 sub same_type {
35 no warnings 'uninitialized';
36 my $class = shift;
37 my $aa = $class->parse_type($_[0]) or confess "Couldn't parse: $_[0]";
38 my $bb = $class->parse_type($_[1]) or confess "Couldn't parse: $_[1]";
39
40 return $aa->simplified eq $bb->simplified;
41 }
42  
43 sub type_extension {
44 my $class = shift;
45 my $type = $class->parse_type($_[0]);
46 croak "Couldn't parse @_" unless $type;
47 return ($type->extensions)[0];
48 }
49  
50 sub parse_type {
51 no warnings 'uninitialized';
52 my $class = shift;
53 my $type = shift;
54 return unless defined $type;
55 return $type if blessed $type && $type->isa("MIME::Type");
56 $type = ".$type" if $type !~ m/\W+/;
57 # Make sure we get stringified version of $type, whatever it is
58 $type .= "";
59 $type = "application/javascript" if $type =~ m{^text/javascript$}i;
60 $type = lc $type;
61 return $class->types->mimeTypeOf($type) || $class->types->type($type);
62 }
63  
64 sub parse_rsc {
65 my $class = shift;
66 my $resource = shift;
67 my ($uri, $dir, $path) = @_;
68 if (ref $resource eq "ARRAY") {
69 ($uri, $dir, $path) = @$resource;
70 }
71 elsif (ref $resource eq "HASH") {
72 ($uri, $dir, $path) = @$resource{qw/uri dir path/};
73 }
74 elsif (blessed $resource) {
75 if ($resource->isa("Path::Resource")) {
76 return $resource->clone;
77 }
78 elsif ($resource->isa("URI::ToDisk")) {
79 $uri = $resource->URI;
80 $dir = $resource->path;
81 }
82 }
83 return Path::Resource->new(uri => $uri, dir => $dir, path => $path);
84 }
85  
86 my @_filters;
87 sub _filters {
88 return @_filters ||
89 grep { ! m/::SUPER$/ } reverse sort __PACKAGE__->filter_load();
90 }
91  
92 sub parse_filter {
93 my $class = shift;
94 my $filter = shift;
95  
96 my $_filter;
97 for my $possible ($class->_filters) {
98 last if $_filter = $possible->new_parse($filter, @_);
99 }
100  
101 return $_filter;
102 }
103  
104 sub _substitute($$$;$$) {
105 my $target = shift;
106 my $character = shift;
107 my $value = shift;
108 my $deprecated = shift;
109 my $original_path = shift;
110  
111 $value = "" unless defined $value;
112  
113 my $found;
114 $found ||= $$target =~ s/\%$character/$value/g;
115 $found ||= $$target =~ s/\%\.$character/$value ? "\.$value" : ""/ge;
116 $found ||= $$target =~ s/\%\-$character/$value ? "\-$value" : ""/ge;
117 $found ||= $$target =~ s/\%\/$character/$value ? "\/$value" : ""/ge;
118  
119 carp "\%$character is deprecated as a path pattern (in \"$original_path\")" if $found && $deprecated;
120 }
121  
122 sub build_output_path {
123 my $class = shift;
124 my $template = shift;
125 my $filter = shift;
126  
127 my $path = $template;
128 $path = $path->{path} if ref $path eq "HASH";
129  
130 return $$path if ref $path eq "SCALAR";
131  
132 $path = '%n%-l%-f%.e' unless $path;
133 $path = "$path/" if blessed $path && $path->isa("Path::Class::Dir");
134 $path .= '%n%-l%-f%.e' if $path && $path =~ m/\/$/;
135 $path .= '%.e' if $path =~ m/(?:^|\/)[^.]+$/;
136  
137 local %_;
138 if (ref $filter eq "HASH") {
139 %_ = %$filter;
140 }
141 else {
142 %_ = (
143 fingerprint => $filter->fingerprint,
144 name => $filter->assets->name,
145 kind => $filter->kind->kind,
146 head => $filter->kind->head,
147 tail => $filter->kind->tail,
148 extension => $filter->kind->extension,
149 );
150 }
151  
152 my $original_path = $path;
153  
154 $path =~ s/%b/%-l/g and carp "\%b is deprecated as a path pattern (in \"$original_path\")";
155  
156 _substitute \$path, e => $_{extension};
157 _substitute \$path, f => $_{fingerprint};
158 _substitute \$path, n => $_{name};
159 _substitute \$path, k => $_{kind};
160 _substitute \$path, h => $_{head};
161 _substitute \$path, l => $_{tail};
162  
163 _substitute \$path, d => $_{fingerprint}, 1 => $original_path;
164 _substitute \$path, D => $_{fingerprint}, 1 => $original_path;
165 _substitute \$path, a => $_{tail}, 1 => $original_path;
166  
167 $path =~ s/%%/%/g;
168  
169 return $path;
170 }
171  
172 1;