AssetPack/Asset.pm

filter: Statementstmt 87.8% 72 / 82 Branchbran 67.8% 19 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 84.2% 16 / 19 Podpod 100% 7 / 7 totaltotal 82.2% 116 / 141 CC 17 SCAR 12.3
CC
17
Cov
82%
path
12.0
FROM_JSON
6.0
content
4.0
CRAP 3.4
« AssetPack/Pipe/Combine.pm ↑ Summary AssetPack/Util.pm »
1 package Mojolicious::Plugin::AssetPack::Asset;
2 16 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 16 ▶use Mojo::Asset::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Mojolicious::Plugin::AssetPack::Util qw(diag has_ro DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 16 my %TAG_TEMPLATE;
10 16 $TAG_TEMPLATE{css} = [qw(link rel stylesheet href)];
11 16 $TAG_TEMPLATE{ico} = [qw(link rel icon href)];
12 16 $TAG_TEMPLATE{js} = [qw(script src)];
13 16 $TAG_TEMPLATE{$_} = [qw(img src)] for qw(gif jpg jpeg png svg);
14 16 $TAG_TEMPLATE{$_} = [qw(source src)] for qw(mp3 mp4 ogg ogv webm);
15  
16 33 ▶has checksum => sub { Mojolicious::Plugin::AssetPack::Util::checksum(shift->content) };
Subroutine __ANON__: called CC=1 SCAR=0.0
17 35 ▶has format => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
18 35 my $self = shift;
19 35 ▶ my $name = $self->url =~ /^https?:/ ? Mojo::URL->new($self->url)->path->[-1] : (split m!(\\|/)!, $self->url)[-1];
Branch: 1 branch
BranchTrueFalseTotal
$self->url =~ /^https?:/u ? : 0 35 35
20  
21 35 ▶ return $name =~ /\.(\w+)$/ ? lc $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\.(\w+)$/u ? : 35 0 35
22 16 };
23  
24 24 ▶has minified => sub { shift->url =~ /\bmin\b/ ? 1 : 0 };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
(shift())->url =~ /\bmin\b/u ? : 0 24 24
25 16 has renderer => undef;
26 24 ▶has tag_for => sub { \&_default_tag_for };
Subroutine __ANON__: called CC=1 SCAR=0.0
27  
28 39 ▶has _asset => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
29 39 my $self = shift;
30 39 ▶ return $self->content(delete $self->{content})->_asset if $self->{content};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content"} 35 4 39
31 4 ▶ return Mojo::Asset::File->new(path => delete $self->{path}) if $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"path"} 4 0 4
32 0 return Mojo::Asset::Memory->new;
33 16 };
34  
35 31 ▶has_ro name => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
36 31 my $self = shift;
37 31 my $name;
38  
39 31 ▶ if ($self->url =~ /^https?:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url =~ /^https?:/u) { } 1 30 31
40 1 my $url = Mojo::URL->new($self->url);
41 1 my $qs = $url->query->to_string;
42 1 $name = $url->path->[-1];
43 1 $qs =~ s!\W!_!g;
44 1 $name =~ s!\.\w+$!!;
45 1 ▶ $name .= "_$qs" if $qs;
Branch: 1 branch
BranchTrueFalseTotal
if $qs 0 1 1
46 }
47 else {
48 30 $name = (split m!(\\|/)!, $self->url)[-1];
49 30 $name =~ s!\.\w+$!!;
50 }
51  
52 31 return $name;
53 16 };
54  
55 16 has_ro 'url';
56  
57 25 ▶sub asset {
Subroutine asset: called CC=2 SCAR=6.9
Pod documented
58 25 my $self = shift;
59 25 my $orig = $self->_asset;
60 25 my $clone = $orig->new;
61  
62 25 ▶ if ($orig->is_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig->is_file) { } 13 12 25
63 13 $clone->cleanup(0)->path($orig->path);
64 }
65 else {
66 12 $clone->auto_upgrade(0)->mtime($orig->mtime)->add_chunk($orig->slurp);
67 }
68  
69 25 return $clone;
70 }
71  
72 102 ▶sub content {
Subroutine content: called CC=4 SCAR=13.9
Pod documented
73 102 my $self = shift;
74 102 ▶ return $self->_asset->slurp unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 52 50 102
75 50 ▶ return $self->_asset($_[0]->_asset) if UNIVERSAL::isa($_[0], __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 6 44 50
76 44 ▶ return $self->_asset($_[0]) if UNIVERSAL::isa($_[0], 'Mojo::Asset');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "Mojo::Asset") 32 12 44
77 12 return $self->_asset(Mojo::Asset::Memory->new->add_chunk($_[0]));
78 }
79  
80 0 ▶sub path {
Subroutine path: not called CC=3 SCAR=24.8
Pod documented
81 0 my $self = shift;
82 0 ▶ return $self->_asset(Mojo::Asset::File->new(path => $_[0])) if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
83 0 ▶ return Mojo::File->new($self->_asset->path) if $self->_asset->isa('Mojo::Asset::File');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_asset->isa("Mojo::Asset::File") 0 0 0
84 0 return undef;
85 }
86  
87 0 ▶sub size { $_[0]->_asset->size }
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
88  
89 82 ▶sub url_for { $_[1]->url_for(assetpack => $_[0]->TO_JSON); }
Subroutine url_for: called CC=1 SCAR=0.0
Pod documented
90  
91 80 ▶sub _default_tag_for {
Subroutine _default_tag_for: called CC=3 SCAR=11.0
92 80 my ($asset, $c, $args, @attrs) = @_;
93 80 my $url = $asset->url_for($c);
94 80 ▶ my @template = @{$TAG_TEMPLATE{$asset->format} || $TAG_TEMPLATE{css}};
Condition66%2/3
$TAG_TEMPLATE{$asset->format} || $TAG_TEMPLATE{'css'}
l!l&&r!l&&!r
7910
MC/DC0%0/2
$TAG_TEMPLATE{$asset->format} || $TAG_TEMPLATE{'css'}
$TAG_TEMPLATE{$asset->format} $TAG_TEMPLATE{'css'}
Truth table66%2/3
A || B
A= $TAG_TEMPLATE{$asset->format}B= $TAG_TEMPLATE{'css'}
ABresult
1X1
011
000
95 80 ▶ splice @template, 1, 0, type => $c->app->types->type($asset->format) if $template[0] eq 'source';
Branch: 1 branch
BranchTrueFalseTotal
if $template[0] eq "source" 40 40 80
96 80 return $c->tag(@template, Mojo::URL->new("$args->{base_url}$url"), @attrs);
97 }
98  
99 0 ▶sub FROM_JSON {
Subroutine FROM_JSON: not called CC=2 SCAR=17.9
Pod documented
100 0 my ($self, $attrs) = @_;
101 0 $self->$_($attrs->{$_}) for grep { defined $attrs->{$_} } qw(checksum format minified);
102 0 $self;
103 }
104  
105 90 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
106 90 return {map { ($_ => $_[0]->$_) } qw(checksum format minified name url)};
107 }
108  
109 16 1;
110  
111 =encoding utf8
112  
113 =head1 NAME
114  
115 Mojolicious::Plugin::AssetPack::Asset - An asset
116  
117 =head1 DESCRIPTION
118  
119 L<Mojolicious::Plugin::AssetPack::Asset> represents an asset.
120  
121 =head1 SYNOPSIS
122  
123 use Mojolicious::Plugin::AssetPack::Asset;
124 my $asset = Mojolicious::Plugin::AssetPack::Asset->new(url => "...");
125  
126 =head1 ATTRIBUTES
127  
128 =head2 checksum
129  
130 $str = $self->checksum;
131 $self = $self->checksum($str);
132  
133 The L<checksum|Mojolicious::Plugin::AssetPack::Util/checksum> of L</content>.
134  
135 =head2 format
136  
137 $str = $self->format;
138 $self = $self->format($str);
139  
140 The format of L</content>. Defaults to the extension of L</url> or empty string.
141  
142 =head2 minified
143  
144 $bool = $self->minified;
145 $self = $self->minified($bool);
146  
147 Will be set to true if either L</url> contains "min" or if a pipe has
148 minified L</content>.
149  
150 =head2 name
151  
152 $str = $self->name;
153  
154 Returns the basename of L</url>, without extension.
155  
156 =head2 renderer
157  
158 $code = $self->renderer;
159 $self = $self->renderer(sub { my ($self, $c) = @_; $c->render(data => "...""); })
160  
161 Can be used to register a custom render method for this asset. This is called
162 by L<Mojolicious::Plugin::AssetPack::Store/serve_asset>.
163  
164 =head2 tag_for
165  
166 $code = $self->tag_for;
167 $self = $self->tag_for(sub { my ($c, \%args, @attrs) = @_; return qq(<link rel="...">) });
168  
169 Used to register a custom tag renderer for this asset. The arguments passed
170 in are:
171  
172 =over 2
173  
174 =item * C<$c>
175  
176 The L<Mojolicious::Controller> object used for this request.
177  
178 =item * C<%args>
179  
180 A hash-ref with "base_url" and
181 L<topic|Mojolicious::Plugin::AssetPack::Pipe/topic>.
182  
183 =item * C<@attrs>
184  
185 The HTML attributes passed in from the template.
186  
187 =item
188  
189 =back
190  
191 =head2 url
192  
193 $str = $self->url;
194  
195 Returns the location of the asset.
196  
197 =head1 METHODS
198  
199 =head2 asset
200  
201 $asset = $self->asset;
202  
203 Returns a new L<Mojo::Asset::File> or L<Mojo::Asset::Memory> object, with the
204 content or path from this object.
205  
206 This method is EXPERIMENTAL.
207  
208 =head2 content
209  
210 $bytes = $self->content;
211 $self = $self->content($bytes);
212 $self = $self->content(Mojo::Asset::Memory->new);
213  
214 Used to get or set the content of this asset. The default will be built from
215 passing L</url> to L<Mojolicious::Plugin::AssetPack::Store/file>.
216  
217 =head2 path
218  
219 $str = $self->path;
220  
221 Returns a L<Mojo::File> object that holds the location to the asset on disk or
222 C<undef> if this asset is in memory.
223  
224 =head2 size
225  
226 $int = $self->size;
227  
228 Returns the size of the asset in bytes.
229  
230 =head2 url_for
231  
232 $url = $self->url_for($c);
233  
234 Returns a L<Mojo::URL> object for this asset. C<$c> need to be a
235 L<Mojolicious::Controller>.
236  
237 =head2 FROM_JSON
238  
239 $self = $self->FROM_JSON($hash_ref);
240  
241 The opposite of L</TO_JSON>. Will set the read/write L</ATTRIBUTES> from the
242 values in C<$hash_ref>.
243  
244 =head2 TO_JSON
245  
246 $hash_ref = $self->FROM_JSON;
247  
248 The opposite of L</FROM_JSON>. Will generate a hash ref from L</ATTRIBUTES>.
249  
250 =head1 SEE ALSO
251  
252 L<Mojolicious::Plugin::AssetPack>.
253  
254 =cut
« AssetPack/Pipe/Combine.pm ↑ Summary AssetPack/Util.pm »