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   168301 use v5.28;
  2         6  
6 2     2   7 use strict;
  2         5  
  2         36  
7 2     2   10 use warnings;
  2         2  
  2         90  
8 2     2   371 use experimental 'signatures';
  2         1165  
  2         11  
9              
10             our $VERSION = '0.34';
11              
12 2     2   727 use Iterator::Flex::Utils ':IterAttrs', 'throw_failure';
  2         5  
  2         385  
13 2     2   11 use Ref::Util;
  2         3  
  2         53  
14 2     2   400 use namespace::clean;
  2         11393  
  2         14  
15              
16 2     2   588 use parent 'Iterator::Flex::Base';
  2         2  
  2         13  
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 6 sub new ( $class, $array, $ ) {
  4         9  
  4         6  
  4         5  
46              
47 4 50       17 throw_failure( parameter => q{argument must be an ARRAY reference} )
48             unless Ref::Util::is_arrayref( $array );
49              
50 4         24 $class->SUPER::new( { array => $array }, {} );
51             }
52              
53 5     5 0 7 sub construct ( $class, $state ) {
  5         7  
  5         9  
  5         7  
54              
55 5 50       13 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         8 = @{$state}{qw[ array prev current next ]};
  5         17  
60              
61 5         12 my $len = @$arr;
62              
63 5 100       13 $next = 0 unless defined $next;
64              
65 5 0 0     14 throw_failure( parameter => q{illegal value for 'prev'} )
      33        
66             if defined $prev && ( $prev < 0 || $prev >= $len );
67              
68 5 50 33     21 throw_failure( parameter => q{illegal value for 'current'} )
      66        
69             if defined $current && ( $current < 0 || $current >= $len );
70              
71 5 50 33     25 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   69 return defined $prev ? $arr->[$prev] : undef;
88             },
89              
90             ( +CURRENT ) => sub {
91 22 100   22   83 return defined $current ? $arr->[$current] : undef;
92             },
93              
94             ( +NEXT ) => sub {
95 23 100   23   67 $next = 0 if $next == $len;
96 23         28 $prev = $current;
97 23         27 $current = $next++;
98 23         78 return $arr->[$current];
99             },
100              
101             ( +FREEZE ) => sub {
102             return [
103 1     1   19 $class,
104             {
105             array => $arr,
106             prev => $prev,
107             current => $current,
108             next => $next,
109             },
110             ];
111             },
112 5         82 };
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__