File Coverage

blib/lib/Template/Alloy/Iterator.pm
Criterion Covered Total %
statement 31 41 75.6
branch 17 24 70.8
condition 2 2 100.0
subroutine 15 18 83.3
pod 0 16 0.0
total 65 101 64.3


line stmt bran cond sub pod time code
1             package Template::Alloy::Iterator;
2              
3             =head1 NAME
4              
5             Template::Alloy::Iterator - Handle foreach iterations
6              
7             =cut
8              
9 10     10   60 use strict;
  10         22  
  10         350  
10 10     10   50 use warnings;
  10         19  
  10         6125  
11              
12             sub new {
13 267     267 0 486 my ($class, $items) = @_;
14 267 50       870 $items = [] if ! defined $items;
15 267 50       1822 if (ref($items) eq 'HASH') {
    50          
    100          
16 0         0 $items = [ map { {key => $_, value => $items->{ $_ }} } sort keys %$items ];
  0         0  
17             } elsif (UNIVERSAL::can($items, 'as_list')) {
18 0         0 $items = $items->as_list;
19             } elsif (ref($items) ne 'ARRAY') {
20 38         107 $items = [$items];
21             }
22 267         4228 return bless [$items, 0], $class;
23             }
24              
25             sub get_first {
26 267     267 0 627 my $self = shift;
27 267 100       404 return (undef, 3) if ! @{ $self->[0] };
  267         901  
28 265         3213 return ($self->[0]->[$self->[1] = 0], undef);
29             }
30              
31             sub get_next {
32 683     683 0 1706 my $self = shift;
33 683 100       1278 return (undef, 3) if ++ $self->[1] > $#{ $self->[0] };
  683         5185  
34 443         1181 return ($self->items->[$self->[1]], undef);
35             }
36              
37 443     443 0 9865 sub items { shift->[0] }
38              
39 160     160 0 811 sub index { shift->[1] }
40              
41 57     57 0 73 sub max { $#{ shift->[0] } }
  57         311  
42              
43 36     36 0 99 sub size { shift->max + 1 }
44              
45 60     60 0 132 sub count { shift->index + 1 }
46              
47 0     0 0 0 sub number { shift->index + 1 }
48              
49 67 100   67 0 201 sub first { (shift->index == 0) || 0 }
50              
51 21   100 21 0 32 sub last { my $self = shift; return ($self->index == $self->max) || 0 }
  21         56  
52              
53 15 100   15 0 48 sub odd { shift->count % 2 ? 1 : 0 }
54              
55 15 100   15 0 37 sub even { shift->count % 2 ? 0 : 1 }
56              
57 15 100   15 0 36 sub parity { shift->count % 2 ? 'odd' : 'even' }
58              
59             sub prev {
60 0     0 0   my $self = shift;
61 0 0         return undef if $self->index <= 0;
62 0           return $self->items->[$self->index - 1];
63             }
64              
65             sub next {
66 0     0 0   my $self = shift;
67 0 0         return undef if $self->index >= $self->max;
68 0           return $self->items->[$self->index + 1];
69             }
70              
71             1;
72              
73             __END__