File Coverage

blib/lib/WWW/Ruler.pm
Criterion Covered Total %
statement 32 32 100.0
branch 19 24 79.1
condition 6 9 66.6
subroutine 5 5 100.0
pod 2 2 100.0
total 64 72 88.8


line stmt bran cond sub pod time code
1             package WWW::Ruler;
2              
3 2     2   40655 use strict;
  2         6  
  2         73  
4 2     2   9 use warnings;
  2         4  
  2         52  
5              
6 2     2   889 use WWW::Ruler::Piece;
  2         3  
  2         840  
7              
8             our $VERSION = '0.12';
9              
10             sub new {
11 1     1 1 16 my ( $class, %opts ) = @_;
12              
13 1   33     14 bless { %opts }, ref($class) || $class;
14             }
15              
16             sub cut_off {
17 6     6 1 23 my ( $self, %opts ) = @_;
18              
19             my (
20 6         7 $page_size,
21             $ruler_size,
22             $page_number,
23             $amount,
24             $left_size,
25             $right_size,
26             $all_pages
27             );
28              
29 6         14 for ( qw( page_size ruler_size page_number amount ) ) {
30 24         1552 eval("\$$_ = " . q{defined( $opts{$_} ) ? $opts{$_} : $self->{$_}});
31             }
32              
33             # A building of ruler line...
34 6         26 my $piece = WWW::Ruler::Piece->new( $amount );
35              
36 6 50       22 return $piece->outside(1)
37             unless $amount;
38              
39 6         16 $all_pages = int( ($amount - 1) / $page_size + 1 );
40              
41 6         8 $left_size = int( ( $ruler_size - 1 ) / 2 );
42 6         7 $right_size = $ruler_size - 1 - $left_size;
43              
44 6 100 100     40 return $piece->outside(1)
45             if ( $page_number > $all_pages || $page_number <= 0 );
46              
47 3 100       19 $piece->add_ruler_item( { type => 'prev_pointer', page_number => $page_number - 1} ) if ( $page_number > 1 );
48 3 100       30 $piece->add_ruler_item( { type => 'page', page_number => 1, ( $page_number == 1 ? ( current_page => 1 ) : () )} );
49 3 50       9 $piece->add_ruler_item( { type => 'ellipsis' } ) if ( $page_number - $left_size > 2 );
50              
51 3 50 66     19 for ( my ($n, $cnt) = ( $page_number - $left_size < 2 ? 2 : $page_number - $left_size, $ruler_size );
52             $n < $all_pages && $cnt > 0;
53             $cnt--, $n++)
54             {
55 3 100       17 $piece->add_ruler_item( { type => 'page', page_number => $n, ( $n == $page_number ? ( current_page => 1 ) : () ) } );
56             }
57              
58 3 50       8 $piece->add_ruler_item( { type => 'ellipsis' } ) if ( $page_number + $right_size < $all_pages - 1 );
59 3 100       22 $piece->add_ruler_item( { type => 'page', page_number => $all_pages, ( $page_number == $all_pages ? ( current_page => 1 ) : () )} ) if $all_pages > 1;
    50          
60 3 100       14 $piece->add_ruler_item( { type => 'next_pointer', page_number => $page_number + 1} ) if ( $page_number < $all_pages );
61              
62             # Calculation of bounds
63 3         9 $piece->{start} = ( $page_number - 1 ) * $page_size;
64 3 100       9 $piece->{end} = $page_number < $all_pages ? $piece->{start} + $page_size - 1 : $amount - 1;
65              
66 3         12 return $piece;
67             }
68              
69             1;
70              
71             __END__