File Coverage

blib/lib/Array/Iterator/BiDirectional.pm
Criterion Covered Total %
statement 39 39 100.0
branch 20 20 100.0
condition n/a
subroutine 10 10 100.0
pod 4 7 57.1
total 73 76 96.0


line stmt bran cond sub pod time code
1              
2             package Array::Iterator::BiDirectional;
3              
4 1     1   74222 use strict;
  1         11  
  1         29  
5 1     1   5 use warnings;
  1         2  
  1         37  
6              
7             our $VERSION = '0.130'; # VERSION
8              
9 1     1   579 use Array::Iterator;
  1         3  
  1         387  
10             our @ISA = qw(Array::Iterator);
11              
12             sub has_previous {
13 17     17 1 99 my ($self, $n) = @_;
14              
15 17 100       48 if(not defined $n) { $n = 1 }
  10 100       15  
    100          
16 1         10 elsif(not $n) { die "has_previous(0) doesn't make sense, did you mean current()?" }
17 1         9 elsif($n < 0) { die "has_previous() with negative argument doesn't make sense, did you mean has_next()?" }
18              
19 15         34 my $idx = $self->_current_index - $n;
20              
21 15 100       63 return ($idx > 0) ? 1 : 0;
22             }
23              
24 10     10 0 1810 sub hasPrevious { my $self = shift; $self->has_previous(@_) }
  10         23  
25              
26             sub previous {
27 5     5 1 1752 my ($self) = @_;
28 5 100       14 (($self->_current_index - 1) > 0)
29             || die "Out Of Bounds : no more elements";
30 4         10 $self->_iterated = 1;
31 4         9 return $self->_getItem($self->_iteratee, --$self->_current_index);
32             }
33              
34             sub get_previous {
35 5     5 1 9 my ($self) = @_;
36 5 100       14 return undef unless (($self->_current_index - 1) > 0);
37 4         13 $self->_iterated = 1;
38 4         11 return $self->_getItem($self->_iteratee, --$self->_current_index);
39             }
40              
41 5     5 0 1932 sub getPrevious { my $self = shift; $self->get_previous(@_) }
  5         12  
42              
43             sub look_back {
44 14     14 1 90 my ($self, $n) = @_;
45              
46 14 100       47 if(not defined $n) { $n = 1 }
  5 100       10  
    100          
47 1         9 elsif(not $n) { die "look_back(0) doesn't make sense, did you mean get_previous()?" }
48 1         9 elsif($n < 0) { die "look_back() with negative argument doesn't make sense, did you mean get_next()?" }
49              
50 12         31 my $idx = $self->_current_index - ($n + 1);
51              
52 12 100       41 return undef unless ($idx > 0);
53 7         21 $self->_iterated = 1;
54 7         17 return $self->_getItem($self->_iteratee, $idx);
55             }
56              
57 4     4 0 9 sub lookBack { my $self = shift; $self->look_back(@_) }
  4         10  
58              
59             1;
60             # ABSTRACT: A subclass of Array::Iterator to allow forwards and backwards iteration
61              
62             __END__