File Coverage

blib/lib/Number/Stars.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1             package Number::Stars;
2              
3 4     4   162372 use strict;
  4         9  
  4         156  
4 4     4   38 use warnings;
  4         7  
  4         295  
5              
6 4     4   2380 use Class::Utils qw(set_params);
  4         52758  
  4         98  
7 4     4   411 use Error::Pure qw(err);
  4         14  
  4         264  
8 4     4   2813 use Mo::utils 0.09 qw(check_number);
  4         24005  
  4         120  
9              
10             our $VERSION = 0.03;
11              
12             # Constructor.
13             sub new {
14 6     6 1 697347 my ($class, @params) = @_;
15              
16             # Create object.
17 6         49 my $self = bless {}, $class;
18              
19             # Number of stars.
20 6         26 $self->{'number_of_stars'} = 10;
21              
22             # Process parameters.
23 6         35 set_params($self, @params);
24              
25             # Check number_of_stars.
26 5         78 check_number($self, 'number_of_stars');
27              
28 4         101 return $self;
29             }
30              
31             # Convert percent number to stars definition.
32             sub percent_stars {
33 6     6 1 3801 my ($self, $percent) = @_;
34              
35 6         46 my $stars_hr = {};
36 6         19 my $star_percent = 100 / $self->{'number_of_stars'};
37 6         22 foreach my $star_id (1 .. $self->{'number_of_stars'}) {
38 53 100       128 if ($percent >= $star_id * $star_percent) {
    100          
39 26         73 $stars_hr->{$star_id} = 'full';
40             } elsif ($percent >= ($star_id * $star_percent) - ($star_percent / 2)) {
41 2         5 $stars_hr->{$star_id} = 'half',
42             } else {
43 25         67 $stars_hr->{$star_id} = 'nothing',
44             }
45             }
46              
47 6         31 return $stars_hr;
48             }
49              
50             __END__