File Coverage

blib/lib/Iterator/Flex/ArrayLike.pm
Criterion Covered Total %
statement 58 58 100.0
branch 13 22 59.0
condition 3 15 20.0
subroutine 15 15 100.0
pod 1 2 50.0
total 90 112 80.3


line stmt bran cond sub pod time code
1             package Iterator::Flex::ArrayLike;
2              
3             # ABSTRACT: ArrayLike Iterator Class
4              
5 2     2   460766 use v5.28;
  2         7  
6 2     2   31 use strict;
  2         4  
  2         70  
7 2     2   8 use warnings;
  2         5  
  2         151  
8 2     2   1036 use experimental 'signatures';
  2         5137  
  2         11  
9              
10             our $VERSION = '0.33';
11              
12 2     2   1571 use Ref::Util;
  2         5072  
  2         119  
13 2     2   1009 use Iterator::Flex::Utils ':IterAttrs', 'resolve_meth';
  2         8  
  2         632  
14 2     2   1039 use namespace::clean;
  2         28083  
  2         14  
15              
16 2     2   1207 use parent 'Iterator::Flex::Base';
  2         247  
  2         41  
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              
46              
47              
48              
49              
50              
51              
52              
53              
54              
55              
56              
57              
58              
59              
60              
61              
62              
63              
64              
65              
66              
67              
68              
69              
70              
71              
72              
73              
74              
75              
76 7     7 1 259858 sub new ( $class, $obj, $pars = {} ) {
  7         17  
  7         11  
  7         15  
  7         15  
77              
78 7 50       32 $class->_croak( parameter => q{argument must be a blessed reference} )
79             unless Ref::Util::is_blessed_ref( $obj );
80              
81 7         52 $class->SUPER::new( { object => $obj }, $pars );
82             }
83              
84 7     7 0 12 sub construct ( $class, $state ) {
  7         13  
  7         9  
  7         9  
85              
86 7 50       20 throw_failure( parameter => q{state must be a HASH reference} )
87             unless Ref::Util::is_hashref( $state );
88              
89             my ( $obj, $prev, $current, $next, $length, $at )
90 7         18 = @{$state}{qw[ object prev current next length at ]};
  7         26  
91              
92 7 50       23 throw_failure( parameter => q{state 'object' argument must be a blessed reference} )
93             unless Ref::Util::is_blessed_ref( $obj );
94              
95 7         54 $length = resolve_meth( $obj, $length, 'length', 'len' );
96              
97 7         20 $at = resolve_meth( $obj, $at, 'at', 'getitem' );
98              
99 7         24 my $len = $obj->$length;
100              
101 7 50       45 $next = 0 unless defined $next;
102              
103 7 0 0     23 throw_failure( parameter => q{illegal value for state 'prev' argument} )
      33        
104             if defined $prev && ( $prev < 0 || $prev >= $len );
105              
106 7 0 0     32 throw_failure( parameter => q{illegal value for state 'current' argument} )
      33        
107             if defined $current && ( $current < 0 || $current >= $len );
108              
109 7 50 33     36 throw_failure( parameter => q{illegal value for state 'next' argument} )
110             if $next < 0 || $next > $len;
111              
112 7         8 my $self;
113              
114             return {
115              
116             ( +_SELF ) => \$self,
117              
118             ( +RESET ) => sub {
119 2     2   4 $prev = $current = undef;
120 2         97 $next = 0;
121             },
122              
123             ( +REWIND ) => sub {
124 2     2   3 $next = 0;
125             },
126              
127             ( +PREV ) => sub {
128 31 100   31   75 return defined $prev ? $obj->$at( $prev ) : undef;
129             },
130              
131             ( +CURRENT ) => sub {
132 29 100   29   64 return defined $current ? $obj->$at( $current ) : undef;
133             },
134              
135             ( +NEXT ) => sub {
136 51 100   51   91 if ( $next == $len ) {
137             # if first time through, set current
138 21 100       64 $prev = $current
139             if !$self->is_exhausted;
140 21         49 return $current = $self->signal_exhaustion;
141             }
142 30         28 $prev = $current;
143 30         32 $current = $next++;
144              
145 30         47 return $obj->$at( $current );
146             },
147 7         148 };
148             }
149              
150              
151             __PACKAGE__->_add_roles( qw[
152             State::Registry
153             Next::ClosedSelf
154             Rewind::Closure
155             Reset::Closure
156             Prev::Closure
157             Current::Closure
158             ] );
159              
160             1;
161              
162             #
163             # This file is part of Iterator-Flex
164             #
165             # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
166             #
167             # This is free software, licensed under:
168             #
169             # The GNU General Public License, Version 3, June 2007
170             #
171              
172             __END__