Assets/Asset.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 64 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 360 CC 38 SCAR 55.2
CC
38
Cov
0%
new
650.0
inline
12.0
uri
6.0
CRAP 249.2
1 package File::Assets::Asset;
2  
3 use warnings;
4 use strict;
5  
6 use File::Assets::Util;
7 use File::Assets::Carp;
8 use File::Assets::Asset::Content;
9  
10 use XML::Tiny;
11 use IO::Scalar;
12 use Object::Tiny qw/type rank attributes hidden rsc outside/;
13 use Scalar::Util qw/blessed/;
14  
15 =head1 SYNPOSIS
16  
17 my $asset = File::Asset->new(base => $base, path => "/static/assets.css");
18 $asset = $assets->include("/static/assets.css"); # Or, like this, usually.
19  
20 print "The rank for asset at ", $asset->uri, " is ", $asset->rank, "\n";
21 print "The file for the asset is ", $asset->file, "\n";
22  
23 =head1 DESCRIPTION
24  
25 A File::Asset object represents an asset existing in both URI-space and file-space (on disk). The asset is usually a .js (JavaScript) or .css (CSS) file.
26  
27 =head1 METHODS
28  
29 =head2 File::Asset->new( base => <base>, path => <path>, [ rank => <rank>, type => <type> ])
30  
31 Creates a new File::Asset. You probably don't want to use this, create a L<File::Assets> object and use $assets->include instead.
32  
33 =cut
34  
35 sub _html_parse ($) {
36 XML::Tiny::parsefile(IO::Scalar->new(shift));
37 }
38  
39 sub new {
40 my $self = bless {}, shift;
41 my $asset = @_ == 1 && ref $_[0] eq "HASH" ? shift : { @_ };
42  
43 my $content = delete $asset->{content};
44 $content = ref $content eq "SCALAR" ? $$content : $content;
45 if (defined $content && $content =~ m/^\s*</) { # Looks like tagged content (<script> or <style>)
46 my $tag = _html_parse \$content;
47 croak "Unable to parse $content" unless $tag && $tag->[0];
48 $tag = $tag->[0];
49 my $type = delete $tag->{attrib}->{type};
50 if (! $type) {
51 if ($tag->{name} =~ m/^script$/i) {
52 $type = "js"
53 }
54 elsif ($tag->{name} =~ m/^style$/i) {
55 $type = "css"
56 }
57 }
58 $asset->{type} = $type unless defined $asset->{type};
59 while (my ($name, $value) = each %{ $tag->{attrib} }) {
60 $asset->{$name} = $value unless exists $asset->{$name};
61 }
62 $content = $tag->{content}->[0]->{content};
63 $content = "" unless defined $content;
64 }
65  
66 my ($path, $rsc, $base, $type) = delete @$asset{qw/path rsc base type/};
67 if (defined $type) {
68 my $_type = $type;
69 $type = File::Assets::Util->parse_type($_type) or croak "Don't understand type ($_type) for this asset";
70 }
71  
72 if ($rsc) {
73 croak "Don't have a type for this asset" unless $type;
74 $self->{rsc} = $rsc;
75 $self->{type} = $type;
76 croak "Can't also specify content and ", $self->rsc->file if defined $content;
77 }
78 elsif ($path && $path =~ m/^https?:\/\// || (blessed $path && $path->isa("URI"))) {
79 my $uri = $self->{uri} = URI->new($path);
80 $self->{type} = $type || do {
81 File::Assets::Util->parse_type($uri->path) or croak "Unable to determine type of $uri";
82 };
83 $self->{outside} = 1;
84 }
85 elsif ($base && $path) {
86 if ($path =~ m/^\//) {
87 $self->{rsc} = $base->clone($path);
88 }
89 else {
90 $self->{rsc} = $base->child($path);
91 }
92 croak "Can't also specify content and ", $self->rsc->file if defined $content;
93 $self->{type} = $type || File::Assets::Util->parse_type($path) or croak "Don't know type for asset ($path)";
94 }
95 elsif (defined $content) {
96 croak "Don't have a type for this asset" unless $type;
97 $self->{type} = $type;
98 $self->{digest} = File::Assets::Util->digest->add($content)->hexdigest;
99 $self->{content} = \$content;
100 }
101 else {
102 croak "Don't know what to do";
103 }
104  
105 my $rank = $self->{rank} = delete $asset->{rank} || 0;
106 croak "Don't understand rank ($rank)" if $rank && $rank =~ m/[^\d\+\-\.]/;
107 $self->{cache} = delete $asset->{cache};
108 $self->{inline} = exists $asset->{inline} ?
109 (delete $asset->{inline} ? 1 : 0) :
110 $self->{content} ? 1 : 0;
111 $self->{attributes} = { %$asset }; # The rest goes here!
112  
113 return $self;
114 }
115  
116 =head2 $asset->uri
117  
118 Returns a L<URI> object represting the uri for $asset
119  
120 =cut
121  
122 sub uri {
123 my $self = shift;
124 return $self->{uri} unless $self->{rsc};
125 return ($self->{uri} ||= $self->rsc->uri)->clone;
126 }
127  
128 =head2 $asset->file
129  
130 Returns a L<Path::Class::File> object represting the file for $asset
131  
132 =cut
133  
134 sub file {
135 my $self = shift;
136 return unless $self->{rsc};
137 return $self->{file} ||= $self->rsc->file;
138 }
139  
140 =head2 $asset->path
141  
142 Returns the path of $asset
143  
144 =cut
145  
146 sub path {
147 my $self = shift;
148 return unless $self->{rsc};
149 return $self->{path} ||= $self->rsc->path;
150 }
151  
152 =head2 $asset->content
153  
154 Returns a SCALAR reference to the content contained in $asset->file
155  
156 =cut
157  
158 sub content {
159 my $self = shift;
160 return $self->{content} || $self->_content->content;
161 }
162  
163 =head2 $asset->write( <content> )
164  
165 Writes <content>, which should be a SCALAR reference, to the file located at $asset->file
166  
167 If the parent directory for $asset->file does not exist yet, this method will create it first
168  
169 =cut
170  
171 sub write {
172 my $self = shift;
173 my $content = shift;
174  
175 my $file = $self->file;
176 my $dir = $file->parent;
177 $dir->mkpath unless -d $dir;
178 $file->openw->print($$content);
179 }
180  
181 =head2 $asset->digest
182  
183 Returns a hex digest for the content of $asset
184  
185 =cut
186  
187 # NOTE: $asset->digest used to return a unique signature for the asset (based off the filename), but this has changed to
188 # now return the actual hex digest of the content of $asset
189  
190 sub digest {
191 my $self = shift;
192 return $self->{digest} || $self->_content->digest;
193 }
194  
195 sub content_digest {
196 my $self = shift;
197 carp "File::Assets::Asset::content_digest is DEPRECATED (use ::digest instead)";
198 return $self->digest;
199 }
200  
201 sub mtime {
202 return 0;
203 carp "File::Assets::Asset::mtime is DEPRECATED";
204 }
205  
206 sub file_mtime {
207 my $self = shift;
208 return 0 unless $self->file;
209 return $self->_content->file_mtime;
210 }
211  
212 sub file_size {
213 my $self = shift;
214 return 0 unless $self->file;
215 return $self->_content->file_size;
216 }
217  
218 sub content_mtime {
219 my $self = shift;
220 return 0 unless $self->file;
221 return $self->_content->content_mtime;
222 }
223  
224 sub content_size {
225 my $self = shift;
226 return length ${ $self->{content} } unless $self->file;
227 return $self->_content->content_size;
228 }
229  
230 sub refresh {
231 my $self = shift;
232 return 0 unless $self->file;
233 return $self->_content->refresh;
234 }
235  
236 sub stale {
237 my $self = shift;
238 return 0 unless $self->file;
239 return $self->_content->stale;
240 }
241  
242 sub _content {
243 my $self = shift;
244 return $self->{_content} ||= do {
245 if (my $cache = $self->{cache}) {
246 $cache->content($self->file);
247 }
248 else {
249 File::Assets::Asset::Content->new($self->file);
250 }
251 };
252 }
253  
254 =head2 $asset->key
255  
256 Returns the unique key for the $asset. Usually the path/filename of the $asset, but for content-based assets returns a value based off of $asset->digest
257  
258 =cut
259  
260 sub key {
261 my $self = shift;
262 return $self->path || $self->uri || ($self->{key} ||= '%' . $self->digest);
263 }
264  
265 =head2 $asset->hide
266  
267 Hide $asset (mark it as hidden). That is, don't include $asset during export
268  
269 =cut
270  
271 sub hide {
272 shift->{hidden} = 1;
273 }
274  
275 =head2 $asset->inline
276  
277 Returns whether $asset is inline (should be embedded into the document) or external.
278  
279 If an argument is given, then it will set whether $asset is inline or not (1 for inline, 0 for external).
280  
281 =cut
282  
283 sub inline {
284 my $self = shift;
285 $self->{inline} = shift() ? 1 : 0 if @_;
286 return $self->{inline};
287 }
288  
289 1;
290  
291 __END__