File Coverage

blib/lib/Tags/HTML/Stars.pm
Criterion Covered Total %
statement 59 59 100.0
branch 15 18 83.3
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 86 89 96.6


line stmt bran cond sub pod time code
1             package Tags::HTML::Stars;
2              
3 4     4   86382 use base qw(Tags::HTML);
  4         27  
  4         2068  
4 4     4   47832 use strict;
  4         11  
  4         77  
5 4     4   19 use warnings;
  4         9  
  4         110  
6              
7 4     4   29 use Class::Utils qw(set_params split_params);
  4         10  
  4         1870  
8 4     4   23 use Error::Pure qw(err);
  4         10  
  4         128  
9 4     4   19 use List::Util qw(max);
  4         1757  
  4         261  
10 4     4   3450 use MIME::Base64;
  4         2469  
  4         222  
11 4     4   27 use Readonly;
  4         7  
  4         2575  
12              
13             # Constants.
14             Readonly::Scalar our $STAR_FULL_FILENAME => 'Star*.svg';
15             Readonly::Scalar our $STAR_HALF_FILENAME => 'Star-.svg';
16             Readonly::Scalar our $STAR_NOTHING_FILENAME => 'StarĀ½.svg';
17             Readonly::Scalar our $IMG_STAR_FULL => encode_base64(<<'END', '');
18            
19            
20            
21             END
22             Readonly::Scalar our $IMG_STAR_HALF => encode_base64(<<'END', '');
23            
24            
25            
26            
27            
28            
29             END
30             Readonly::Scalar our $IMG_STAR_NOTHING => encode_base64(<<'END', '');
31            
32            
33            
34             END
35              
36             our $VERSION = 0.05;
37              
38             # Constructor.
39             sub new {
40 6     6 1 3291 my ($class, @params) = @_;
41              
42             # No CSS support.
43 6         17 push @params, 'no_css', 1;
44              
45             # Create object.
46 6         22 my ($object_params_ar, $other_params_ar) = split_params(
47             ['public_image_dir', 'star_width'], @params);
48 6         224 my $self = $class->SUPER::new(@{$other_params_ar});
  6         29  
49              
50             # Public image directory.
51 5         204 $self->{'public_image_dir'} = undef;
52              
53             # Star width (300px).
54 5         9 $self->{'star_width'} = undef;
55              
56             # Process params.
57 5         10 set_params($self, @{$object_params_ar});
  5         15  
58              
59             # Object.
60 5         82 return $self;
61             }
62              
63             # Process 'Tags'.
64             sub _process {
65 3     3   44 my ($self, $stars_hr) = @_;
66              
67             # Main stars.
68 3         13 $self->{'tags'}->put(
69             ['b', 'div'],
70             );
71              
72 3         178 my $max_num = max(keys %{$stars_hr});
  3         20  
73 3         11 foreach my $num (1 .. $max_num) {
74 9         2163 my $image_src = $self->_star_src($stars_hr->{$num});
75              
76             $self->{'tags'}->put(
77             ['b', 'img'],
78             (
79             $self->{'star_width'}
80             ? (
81 9 100       49 ['a', 'style', 'width: '.$self->{'star_width'}.';'],
82             ) : (),
83             ),
84             ['a', 'src', $image_src],
85             ['e', 'img'],
86             );
87             }
88              
89 3         951 $self->{'tags'}->put(
90             ['e', 'div'],
91             );
92              
93 3         148 return;
94             }
95              
96             sub _star_src {
97 9     9   18 my ($self, $id) = @_;
98              
99             # Link to file.
100 9         13 my $src;
101 9 100       21 if (defined $self->{'public_image_dir'}) {
102 3         6 $src = $self->{'public_image_dir'};
103 3 50       9 if ($src ne '') {
104 3         7 $src .= '/';
105             }
106 3 100       13 if ($id eq 'full') {
    100          
    50          
107 1         2 $src .= $STAR_FULL_FILENAME;
108             } elsif ($id eq 'half') {
109 1         2 $src .= $STAR_HALF_FILENAME;
110             } elsif ($id eq 'nothing') {
111 1         2 $src .= $STAR_NOTHING_FILENAME
112             }
113             } else {
114 6         10 $src = 'data:image/svg+xml;base64,';
115 6 100       23 if ($id eq 'full') {
    100          
    50          
116 2         5 $src .= $IMG_STAR_FULL;
117             } elsif ($id eq 'half') {
118 2         8 $src .= $IMG_STAR_HALF;
119             } elsif ($id eq 'nothing') {
120 2         5 $src .= $IMG_STAR_NOTHING;
121             }
122             }
123              
124 9         23 return $src;
125             }
126              
127             1;
128              
129             __END__