File Coverage

blib/lib/Web/AssetLib/Asset.pm
Criterion Covered Total %
statement 22 35 62.8
branch 0 10 0.0
condition n/a
subroutine 8 9 88.8
pod n/a
total 30 54 55.5


line stmt bran cond sub pod time code
1             package Web::AssetLib::Asset;
2              
3 7     7   4404843 use Method::Signatures;
  7         48011  
  7         45  
4 7     7   2532 use Moose;
  7         279246  
  7         32  
5 7     7   25122 use MooseX::Aliases;
  7         5933  
  7         19  
6 7     7   221733 use Data::Dumper;
  7         13707  
  7         418  
7 7     7   34 use Digest::MD5 'md5_hex';
  7         11  
  7         303  
8 7     7   689 use Web::AssetLib::Util;
  7         14  
  7         1657  
9              
10             has 'rank' => (
11             is => 'rw',
12             isa => 'Num',
13             default => 0
14             );
15              
16             # javascript, css
17             has 'type' => (
18             is => 'ro',
19             isa => 'Str',
20             required => 1
21             );
22              
23             around 'type' => sub {
24             my ( $orig, $self ) = @_;
25              
26             return Web::AssetLib::Util::normalizeFileType( $self->$orig );
27             };
28              
29             # the engine that knows what to do
30             has 'input_engine' => (
31             is => 'rw',
32             isa => 'Str',
33             default => 'LocalFile'
34             );
35              
36             has 'input_args' => (
37             is => 'rw',
38             isa => 'HashRef',
39             default => sub { {} }
40             );
41              
42             has 'isPassthru' => (
43             is => 'rw',
44             isa => 'Bool',
45             default => 0
46             );
47              
48             has 'default_html_attrs' => (
49             is => 'rw',
50             isa => 'HashRef',
51             default => sub { {} }
52             );
53              
54             # private attrs:
55              
56             has 'fingerprint' => (
57             is => 'ro',
58             isa => 'Str',
59             lazy => 1,
60             default => sub {
61             my $self = shift;
62             local $Data::Dumper::Terse = 1;
63             my $string = sprintf( '%s%s%s',
64             $self->type, $self->input_engine, Dumper( $self->input_args ) );
65             $string =~ s/\s//g;
66             return md5_hex $string;
67             }
68             );
69              
70             has 'contents' => (
71             is => 'ro',
72             isa => 'Str',
73             writer => 'set_contents'
74             );
75              
76             has 'digest' => (
77             is => 'ro',
78             isa => 'Str',
79             writer => 'set_digest'
80             );
81              
82             has 'link_path' => (
83             is => 'rw',
84             isa => 'Str'
85             );
86              
87             has 'output' => (
88             is => 'rw',
89             isa => 'Maybe[Web::AssetLib::Output]'
90             );
91              
92             has 'isCompiled' => (
93             is => 'ro',
94             isa => 'Bool',
95             writer => '_set_isCompiled',
96             default => 0
97             );
98              
99 7 0   7   25926 method as_html ( :$html_attrs = {} ) {
  0 0   0      
  0 0          
  0            
  0            
  0            
  0            
100 0 0         $self->log->warn('attempting to generate html before asset is compiled')
101             unless $self->isCompiled;
102              
103 0           $html_attrs = { %{ $self->default_html_attrs }, %$html_attrs };
  0            
104              
105 0           my $tag;
106 0 0         if ( $self->output ) {
107 0           $tag = Web::AssetLib::Util::generateHtmlTag(
108             output => $self->output,
109             html_attrs => $html_attrs
110             );
111             }
112              
113 0           return $tag;
114             }
115              
116 7     7   1380 no Moose;
  7         9  
  7         44  
117              
118             1;
119              
120             =pod
121            
122             =encoding UTF-8
123            
124             =head1 NAME
125              
126             Web::AssetLib::Asset - a representation of a particular asset in your library
127              
128             =head1 SYNOPSIS
129              
130             my $asset = Web::AssetLib::Asset->new(
131             type => 'javascript',
132             input_engine => 'LocalFile',
133             rank => -100,
134             input_args => { path => "your/local/path/jquery.min.js", }
135             );
136              
137             =head1 ATTRIBUTES
138            
139             =head2 type (required)
140            
141             File type string. Currently supports: js, javascript, css, stylesheet, jpeg, jpg
142              
143             =head2 input_engine
144            
145             string; partial class name that will match one of the provided input_engines for your library (defaults to "LocalFile")
146              
147             =head2 rank
148            
149             number; Assets added to bundles will be exported in the order in which they are added. If an
150             asset should be exported in a different order, provide a rank. Lower numbers will
151             result in the asset being compiled earlier, and higher numbers will result in the asset
152             being compiled later. (defaults to 0)
153              
154             =head2 input_args
155            
156             hashref; a place to store arguments that the various input plugins may
157             require for a given asset (see input plugin docs for specific requirements)
158              
159             =head2 isPassthru
160            
161             boolean; assets marked isPassthru will not be minified or concatenated
162              
163             =head2 default_html_attrs
164            
165             hashref; provides html attribute defaults for C<< as_html() >>
166              
167             =head1 METHODS
168              
169             =head2 set_digest
170              
171             $asset->set_digest( $digest );
172            
173             Stores the digest in the Asset object. (It is required that this value be set,
174             if writing your own input engine.)
175              
176             =head2 set_contents
177              
178             $asset->set_contents( $contents );
179            
180             Stores the file contents in the Asset object. (It is required that this value be set,
181             if writing your own input engine.)
182              
183             =head2 fingerprint
184              
185             my $fingerprint = $asset->fingerprint();
186            
187             (Somewhat) unique identifier for asset, created by concatenating and hashing: type, input_engine, and input_args.
188             Helpful to identify asset uniqueness prior to opening and reading the file.
189              
190             =head2 digest
191              
192             my $digest = $asset->digest();
193            
194             Truly unique identifier for asset - an MD5 file digest. Only available after the
195             file has been opened and read (compile() has been called on asset), otherwise returns undef.
196              
197             =head2 as_html
198              
199             my $html_tag = $asset->as_html( html_attrs => { async => 'async' } );
200            
201             Returns an HTML-formatted string linking to asset's output location. Only available after the
202             file has been opened and read (compile() has been called on asset), otherwise returns undef.
203              
204             =head1 AUTHOR
205            
206             Ryan Lang <rlang@cpan.org>
207              
208             =cut