Controller/AutoAssets/Handler/ImageSet.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 85 SCAR 30.0
CC
4
Cov
0%
rebuild_manifest
12.0
_unflatten_path
6.0
img_tag
2.0
CRAP 20.0
1 package Catalyst::Controller::AutoAssets::Handler::ImageSet;
2 use strict;
3 use warnings;
4  
5 # VERSION
6  
7 use Moose;
8 use namespace::autoclean;
9  
10 extends 'Catalyst::Controller::AutoAssets::Handler::Directory';
11  
12 # Whether or not to include parent directories in the image name if this is true,
13 # a warning will be thrown if there are any duplicate filenames (and only the
14 # first will be used)
15 has 'flatten_paths', is => 'ro', isa => 'Bool', default => 0;
16  
17 has 'include_regex', is => 'ro', isa => 'Maybe[Str]', default => '\.(png|jpg|jpeg|gif|bmp|tif|tiff|svg)$';
18 has 'regex_ignore_case', is => 'ro', isa => 'Bool', default => 1;
19  
20 has 'manifest', is => 'rw', isa => 'HashRef', default => sub {{}};
21 sub rebuild_manifest {
22 my $self = shift;
23
24 my $manifest = {};
25 for my $path (sort keys %{$self->subfile_meta}) {
26 my $name = $path;
27 # strip parent directories if flatten_paths is on:
28 ($name) = reverse split(/\//,$path) if($self->flatten_paths);
29 if(exists $manifest->{$name}) {
30 $self->_app->log->warn("Not including duplicate filename '$name' ('$path')");
31 next;
32 }
33 # This is being done this way to make it easy for derived classes to put
34 # additional data in the manifest associated with each item
35 $manifest->{$name} = { subfile => $path };
36 }
37
38 $self->manifest($manifest);
39 }
40 after set_subfile_meta => sub { (shift)->rebuild_manifest };
41  
42  
43 around asset_request => sub {
44 my ( $orig, $self, $c, $sha1, @args ) = @_;
45
46 ## Just for debugging:
47 #if($sha1 eq 'dump_manifest') {
48 # $self->clear_asset;
49 # $self->prepare_asset;
50 #
51 # use Data::Dumper::Concise 'Dumper';
52 # $c->response->header('Content-Type' => 'text/plain');
53 # $c->response->body(
54 # $self->asset_name . ': ' . scalar(keys %{$self->manifest}) . " Items\n\n" .
55 # Dumper($self->manifest)
56 # );
57 # return $c->detach;
58 #}
59
60 # If we wanted both/either the flattened and orig paths to work we could
61 # call $self->_unflatten_path(join('/',@args)) instead below
62 return $self->$orig($c, $sha1, $self->_unflatten_path($args[0]))
63 if ($self->flatten_paths);
64  
65 return $self->$orig($c, $sha1, @args);
66 };
67  
68 sub _unflatten_path {
69 my ($self, $name) = @_;
70 return undef unless (defined $name);
71 my $itm = $self->manifest->{$name} or return $name;
72 return $itm->{subfile};
73 }
74  
75 around _subfile_mtime_verify => sub {
76 my ($orig, $self, $path) = @_;
77 return $self->$orig($self->_unflatten_path($path));
78 };
79  
80 around asset_path => sub {
81 my ($orig, $self, @subpath) = @_;
82 return $self->flatten_paths
83 ? join('/',$self->base_path,$self->asset_name,@subpath)
84 : $self->$orig(@subpath);
85 };
86  
87  
88 has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw(
89 built_mtime
90 inc_mtimes
91 last_fingerprint_calculated
92 subfile_meta
93 _excluded_paths
94 manifest
95 )]};
96  
97  
98 sub img_tag {
99 my ($self, @path) = @_;
100 my $name = join('/',@path);
101 return '<img alt="' . $name .
102 '" src="' . $self->asset_path($name) . '">';
103 }
104  
105 sub img_tag_title {
106 my ($self, @path) = @_;
107 my $name = join('/',@path);
108 return '<img alt="' . $name . '" title="' . $name .
109 '" src="' . $self->asset_path($name) . '">';
110 }
111  
112  
113 sub dump_all_img_tags {
114 my $self = shift;
115 return join("\n",map { $self->img_tag_title($_) } (sort keys %{$self->manifest}));
116 }
117  
118  
119 1;
120  
121 __END__
122  
123 =pod
124  
125 =head1 NAME
126  
127 Catalyst::Controller::AutoAssets::Handler::ImageSet - ImageSet type handler
128  
129 =head1 SYNOPSIS
130  
131 In your controller:
132  
133 package MyApp::Controller::Assets::MyImages;
134 use parent 'Catalyst::Controller::AutoAssets';
135
136 1;
137  
138 Then, in your .conf:
139  
140 <Controller::Assets::MyImages>
141 include root/images/
142 type ImageSet
143 flatten_paths 1
144 include_regex '\.(png|jpg|gif)$'
145 </Controller::Assets::MyImages>
146  
147 And in your .tt files:
148  
149 [% c.controller('Assets::MyImages').img_tag('foo.png') %]
150
151 <img src="[% c.controller('Assets::MyImages').asset_path('apple.jpg') %]">
152  
153 Or, in static HTML:
154  
155 <img src="/assets/myimages/current/apple.jpg">
156  
157 =head1 DESCRIPTION
158  
159 Like the 'Directory' asset type but with some extra options, defaults and functionality specific to images.
160  
161 This class extends L<Catalyst::Controller::AutoAssets::Handler::Directory>. Only differences are shown below.
162  
163 =head1 CONFIG PARAMS
164  
165 =head2 include_regex
166  
167 Optional regex ($string) to require files to match to be included.
168  
169 Defaults to C<'\.(png|jpg|jpeg|gif|bmp|tif|tiff|svg)$'>
170  
171 =head2 regex_ignore_case
172  
173 Whether or not to use case-insensitive regex (C<qr/$regex/i> vs C<qr/$regex/>) when evaluating
174 include_regex/exclude_regex.
175  
176 Defaults to true (1).
177  
178 =head2 flatten_paths
179  
180 Whether or not to convert paths to filenames (i.e. C<'path/to/apple.jpg'> becomes C<'apple.jpg'>) for shorter
181 and easier names. Duplicate filenames ignored (only the first will be used) with a warning in the log.
182  
183 Defaults to false (0).
184  
185 =head1 METHODS
186  
187 =head2 img_tag
188  
189 Convenience method to return an HTML img tag for the supplied image/path.
190  
191 =head2 img_tag_title
192  
193 Convenience method to return an HTML img tag for the supplied image/path with the 'title' attribute
194 (i.e. for mouse-over) set to the path.
195  
196 =head2 dump_all_img_tags
197  
198 Dumps img tags for every image in the asset
199  
200 =head1 SEE ALSO
201  
202 =over
203  
204 =item L<Catalyst::Controller::AutoAssets>
205  
206 =item L<Catalyst::Controller::AutoAssets::Handler>
207  
208 =item L<Catalyst::Controller::AutoAssets::Handler::Directory>
209  
210 =item L<Catalyst::Controller::AutoAssets::Handler::IconSet>
211  
212 =back
213  
214 =head1 AUTHOR
215  
216 Henry Van Styn <vanstyn@cpan.org>
217  
218 =head1 COPYRIGHT AND LICENSE
219  
220 This software is copyright (c) 2013 by IntelliTree Solutions llc.
221  
222 This is free software; you can redistribute it and/or modify it under
223 the same terms as the Perl 5 programming language system itself.
224  
225 =cut