Controller/AutoAssets/Handler/IconSet.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 99 SCAR 17.9
CC
2
Cov
0%
icon_class
6.0
rebuild_css_file
2.0
html_head_tags
2.0
CRAP 6.0
1 package Catalyst::Controller::AutoAssets::Handler::IconSet;
2 use strict;
3 use warnings;
4  
5 # VERSION
6  
7 use Moose;
8 use namespace::autoclean;
9  
10 extends 'Catalyst::Controller::AutoAssets::Handler::ImageSet';
11  
12 has 'flatten_paths', is => 'ro', isa => 'Bool', default => 1;
13 has 'icon_name_prefix', is => 'ro', isa => 'Str', default => 'icon-';
14 has 'lowercase_icon_names', is => 'ro', isa => 'Bool', default => 1;
15 has 'strip_ext_icon_names', is => 'ro', isa => 'Bool', default => 1;
16 has 'replace_nonwords_icon_names', is => 'ro', isa => 'Bool', default => 1;
17 has 'css_file_name', is => 'ro', isa => 'Str', default => 'icons.css';
18  
19 around asset_request => sub {
20 my ( $orig, $self, $c, $sha1, @args ) = @_;
21
22 # Request for the generated CSS
23 if($args[0] eq $self->css_file_name) {
24 $self->prepare_asset;
25 return $self->unknown_asset($c) unless ($sha1 eq $self->asset_name);
26 return $self->_set_file_response($c,$self->css_file,'text/css');
27 }
28
29 return $self->$orig($c, $sha1, @args);
30 };
31  
32 has 'icon_name_generator', is => 'ro', isa => 'CodeRef', lazy => 1, default => sub {
33 return sub {
34 my ($self, $name) = @_;
35 $name =~ s/\.\w+$// if ($self->strip_ext_icon_names);
36 $name =~ s/[\_\W]/\-/g if($self->replace_nonwords_icon_names);
37 $name = lc($name) if ($self->lowercase_icon_names);
38 return $self->icon_name_prefix . $name;
39 };
40 };
41  
42 has 'css_rule_generator', is => 'ro', isa => 'CodeRef', lazy => 1, default => sub {
43 return sub {
44 my ($self, $icon, $path) = @_;
45 return '.' . $icon . ' { background-image: url(' . $path . ') !important; }';
46 };
47 };
48  
49  
50 has 'css_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
51 my $self = shift;
52 return $self->work_dir->file($self->css_file_name);
53 };
54  
55 has 'icon_manifest', is => 'rw', isa => 'HashRef', default => sub {{}};
56 after rebuild_manifest => sub {
57 my $self = shift;
58 my $manifest = $self->manifest;
59
60 my %icon_map = ();
61 for my $name (keys %{$self->manifest}) {
62 my $icon = $self->icon_name_generator->($self,$name);
63 die "Duplicate icon name '$icon' ($name)" if (exists $icon_map{$icon});
64
65 # The path in the CSS can be relative because the css file itself
66 # is served from the root of the asset path. That is good because
67 # we would have a deep recursion problem if we wanted to get the
68 # asset path here (i.e. $self->asset_path($name) ) because our caller
69 # might already be: asset_path -> prepare_asset -> ... -> us
70 $self->manifest->{$name}->{css_rule} = $self->css_rule_generator->($self,$icon,$name);
71 $self->manifest->{$name}->{name} = $name;
72 $self->manifest->{$name}->{icon_name} = $icon;
73
74 $icon_map{$icon} = $self->manifest->{$name};
75 }
76
77 $self->icon_manifest(\%icon_map);
78 $self->rebuild_css_file;
79 };
80  
81 sub rebuild_css_file {
82 my $self = shift;
83
84 my $css = join("\n",map {
85 $self->manifest->{$_}->{css_rule}
86 } (sort keys %{$self->manifest}) );
87
88 return $self->css_file->spew($css);
89 }
90  
91 sub html_head_tags {
92 my $self = shift;
93 return
94 "<!-- AUTO GENERATED BY " . ref($self->Controller) . " (/" .
95 $self->action_namespace($self->_app) . ") -->\r\n" .
96 '<link rel="stylesheet" type="text/css" href="' .
97 join('/',$self->asset_path,$self->css_file_name) .
98 '" />' .
99 "\r\n<!-- ---- END AUTO GENERATED ASSETS ---- -->\r\n";
100 }
101  
102 sub icon_class {
103 my ($self, @path) = @_;
104 die "icon_class_name() requires subfile path argument"
105 unless (scalar @path > 0);
106
107 # Will prepare the asset if needed:
108 $self->asset_path(@path);
109
110 my $name = join('/',@path);
111 my $data = $self->manifest->{$name} or die "No such image/icon '$name'";
112 return $data->{icon_name};
113 }
114  
115 has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw(
116 built_mtime
117 inc_mtimes
118 last_fingerprint_calculated
119 subfile_meta
120 _excluded_paths
121 manifest
122 icon_manifest
123 )]};
124  
125  
126  
127 1;
128  
129 __END__
130  
131 =pod
132  
133 =head1 NAME
134  
135 Catalyst::Controller::AutoAssets::Handler::IconSet - IconSet type handler
136  
137 =head1 SYNOPSIS
138  
139 In your controller:
140  
141 package MyApp::Controller::Assets::Icons;
142 use parent 'Catalyst::Controller::AutoAssets';
143
144 1;
145  
146 Then, in your .conf:
147  
148 <Controller::Assets::Icons>
149 include root/static/icons/
150 type IconSet
151 </Controller::Assets::Icons>
152  
153 And in your .tt files:
154  
155 <head>
156 <!-- include the auto generated icon CSS -->
157 [% c.controller('Assets::Icons').html_head_tags %]
158
159 <!-- or, in static HTML -->
160 <link rel="stylesheet" type="text/css" href="/assets/icons/current/icons.css" />
161 </head>
162
163 ...
164
165 <!-- access individual icons by name -->
166 [% c.controller('Assets::Icons').img_tag('foo.png') %]
167 <img src="[% c.controller('Assets::Icons').asset_path('apple.jpg') %]">
168
169 ...
170
171 <!-- use icon class names in markup -->
172 <div class="mycls [% c.controller('Assets::Icons').icon_class('apple.jpg') %]"></div>
173
174 <!-- or, in static HTML -->
175 <div class="mycls icon-apple"></div>
176  
177 =head1 DESCRIPTION
178  
179 Like the 'ImageSet' asset type but also generates/includes CSS classes for accessing/using icons in HTML
180 markup (i.e. with the css style C<'background-image'>). These icon class names can then be used in HTML tags
181 or directly in JavaScript frameworks like ExtJS.
182  
183 This class extends L<Catalyst::Controller::AutoAssets::Handler::ImageSet>. Only differences are shown below.
184  
185 =head1 CONFIG PARAMS
186  
187 =head2 flatten_paths
188  
189 Inherited from ImageSet but defaults to true (1).
190  
191 =head2 icon_name_prefix
192  
193 String to prepend to the generated css icon class name. Defaults to C<'icon-'>.
194  
195 =head2 lowercase_icon_names
196  
197 Whether or not to use all lowercase characters in the css icon class name. Defaults to true (1).
198  
199 =head2 strip_ext_icon_names
200  
201 Whether or not to strip the file extension from the filename when generating the css class name.
202 Defaults to true (1).
203  
204 =head2 replace_nonwords_icon_names
205  
206 Whether or not to convert special characters and _ in filename to '-' when generating the css class name.
207 Defaults to true (1).
208  
209 =head2 icon_name_generator
210  
211 CodeRef used to generate the css icon class name. Receives ($self, $image_name) as arguments and returns
212 the css class name. If this is overridden it will supersede all the above "_icon_names" options. With all
213 the default options, the $image_name C<'Brick_House.png'> would return the CSS class name C<'icon-brick-house'>.
214  
215 =head2 css_rule_generator
216  
217 CodeRef used to generate the actual css rule definition for a given icon class name.
218 Receives ($self, $icon_class, $path) as arguments and returns the css rule string. By default, args C<'icon-brick-house'>
219 and C<'Brick_House.png'> would return:
220  
221 .icon-brick-house { background-image: url(Brick_House.png) !important; }
222  
223 =head2 css_file_name
224  
225 The name of the automatically generated CSS file which will contain the CSS rules for all the icons included in
226 the asset. This file is accessible as an ordinary subfile in the asset.
227  
228 Defaults to C<'icons.css'>.
229  
230 =head1 METHODS
231  
232 =head2 html_head_tags
233  
234 Returns a pre-built link tag to include the generated CSS suitable for use in an HTML <head>
235 section. By default this will be:
236  
237 <link rel="stylesheet" type="text/css" href="<CONTROLLER_PATH>/<SHA1>/icons.css" />
238  
239 =head2 icon_class
240  
241 Returns the generated icon css class name for the supplied icon path/filename.
242  
243 [% c.controller('Assets::Icons').icon_class("Brick_House.png") %] # 'icon-brick-house'
244  
245 =head1 SEE ALSO
246  
247 =over
248  
249 =item L<Catalyst::Controller::AutoAssets>
250  
251 =item L<Catalyst::Controller::AutoAssets::Handler::ImageSet>
252  
253 =item L<Catalyst::Controller::AutoAssets::Handler>
254  
255 =back
256  
257 =head1 AUTHOR
258  
259 Henry Van Styn <vanstyn@cpan.org>
260  
261 =head1 COPYRIGHT AND LICENSE
262  
263 This software is copyright (c) 2013 by IntelliTree Solutions llc.
264  
265 This is free software; you can redistribute it and/or modify it under
266 the same terms as the Perl 5 programming language system itself.
267  
268 =cut