File Coverage

blib/lib/Iterator/Flex/Cycle.pm
Criterion Covered Total %
statement 52 52 100.0
branch 12 18 66.6
condition 5 15 33.3
subroutine 16 16 100.0
pod 1 2 50.0
total 86 103 83.5


line stmt bran cond sub pod time code
1             package Iterator::Flex::Cycle;
2              
3             # ABSTRACT: Array Cycle Iterator Class
4              
5 2     2   355122 use v5.28;
  2         7  
6 2     2   10 use strict;
  2         4  
  2         42  
7 2     2   13 use warnings;
  2         9  
  2         124  
8 2     2   623 use experimental 'signatures';
  2         1926  
  2         12  
9              
10             our $VERSION = '0.33';
11              
12 2     2   1027 use Iterator::Flex::Utils ':IterAttrs';
  2         7  
  2         558  
13 2     2   14 use Ref::Util;
  2         4  
  2         80  
14 2     2   683 use namespace::clean;
  2         18454  
  2         16  
15              
16 2     2   796 use parent 'Iterator::Flex::Base';
  2         4  
  2         16  
17              
18              
19              
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32              
33              
34              
35              
36              
37              
38              
39              
40              
41              
42              
43              
44              
45 4     4 1 5 sub new ( $class, $array, $ ) {
  4         7  
  4         5  
  4         4  
46              
47 4 50       12 throw_failure( parameter => q{argument must be an ARRAY reference} )
48             unless Ref::Util::is_arrayref( $array );
49              
50 4         21 $class->SUPER::new( { array => $array }, {} );
51             }
52              
53 5     5 0 7 sub construct ( $class, $state ) {
  5         8  
  5         7  
  5         7  
54              
55 5 50       12 throw_failure( parameter => q{state must be a HASH reference} )
56             unless Ref::Util::is_hashref( $state );
57              
58             my ( $arr, $prev, $current, $next )
59 5         7 = @{$state}{qw[ array prev current next ]};
  5         14  
60              
61 5         11 my $len = @$arr;
62              
63 5 100       11 $next = 0 unless defined $next;
64              
65 5 0 0     10 throw_failure( parameter => q{illegal value for 'prev'} )
      33        
66             if defined $prev && ( $prev < 0 || $prev >= $len );
67              
68 5 50 33     17 throw_failure( parameter => q{illegal value for 'current'} )
      66        
69             if defined $current && ( $current < 0 || $current >= $len );
70              
71 5 50 33     22 throw_failure( parameter => q{illegal value for 'next'} )
72             if $next < 0 || $next > $len;
73              
74              
75             return {
76              
77             ( +RESET ) => sub {
78 1     1   1 $prev = $current = undef;
79 1         2 $next = 0;
80             },
81              
82             ( +REWIND ) => sub {
83 1     1   2 $next = 0;
84             },
85              
86             ( +PREV ) => sub {
87 22 100   22   53 return defined $prev ? $arr->[$prev] : undef;
88             },
89              
90             ( +CURRENT ) => sub {
91 22 100   22   58 return defined $current ? $arr->[$current] : undef;
92             },
93              
94             ( +NEXT ) => sub {
95 23 100   23   38 $next = 0 if $next == $len;
96 23         18 $prev = $current;
97 23         24 $current = $next++;
98 23         64 return $arr->[$current];
99             },
100              
101             ( +FREEZE ) => sub {
102             return [
103 1     1   16 $class,
104             {
105             array => $arr,
106             prev => $prev,
107             current => $current,
108             next => $next,
109             },
110             ];
111             },
112 5         68 };
113             }
114              
115              
116             __PACKAGE__->_add_roles( qw[
117             State::Registry
118             Next::Closure
119             Rewind::Closure
120             Reset::Closure
121             Prev::Closure
122             Current::Closure
123             Freeze
124             ] );
125              
126              
127             1;
128              
129             #
130             # This file is part of Iterator-Flex
131             #
132             # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
133             #
134             # This is free software, licensed under:
135             #
136             # The GNU General Public License, Version 3, June 2007
137             #
138              
139             __END__