Controller/AutoAssets/Handler/IconSet.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 100 CC 2 SCAR 11.0
CC
2
Cov
0%
icon_class
6.0
rebuild_css_file
2.0
html_head_tags
2.0
CRAP 3.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__